Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBean committed Jul 27, 2022
0 parents commit f5dbac9
Show file tree
Hide file tree
Showing 45 changed files with 1,488 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
55 changes: 55 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

**Happy coding**!
3 changes: 3 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

If you discover any security related issues, please email security@justbetter.nl instead of using the issue tracker.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
composer.lock
.phpunit.result.cache
16 changes: 16 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The MIT License (MIT)

Copyright (c) JustBetter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
229 changes: 229 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# Dynamics Client

This package will connect you to your Microsoft Dynamics web services via OData. Custom web services can easily be
implemented and mapped to your liking.

The way we interact with OData has been inspired by Laravel's Query Builder.

```php
$customer = Customer::query()->findOrFail('1000');

$customer->update([
'Name' => 'John Doe',
]);

$customers = Customer::query()
->where('City', '=', 'Alkmaar')
->lazy();

$items = Item::query()
->whereIn('No', ['1000', '2000'])
->get();

$customer = Customer::new()->create([
'Name' => 'Jane Doe',
])
```

## Installation

Install the composer package.

```shell
composer require justbetter/laravel-dynamics-client
```

## Setup

Publish the configuration of the package.

```shell
php artisan vendor:publish --provider="JustBetter\DynamicsClient\ServiceProvider" --tag=config
```

## Configuration

Add your Dynamics credentials in the `.env`:

```
DYNAMICS_BASE_URL=https://127.0.0.1:7048/DYNAMICS
DYNAMICS_VERSION=ODataV4
DYNAMICS_COMPANY=
DYNAMICS_USERNAME=
DYNAMICS_PASSWORD=
DYNAMICS_PAGE_SIZE=1000
```

Be sure the `DYNAMICS_PAGE_SIZE` is set equally to the `Max Page Size` under `OData Services` in the configuration of
Dynamics. This is crucial for the functionalities of the `lazy` method of the `QueryBuilder`.

### Authentication

> **Note:** Be sure that Dynamics has been properly configured for OData.
This package uses NTLM authentication by default. If you are required to use basic auth you can change this in
your `.env`.

```
DYNAMICS_AUTH=basic
```

| Type | Authentication Type |
|------------|----------------------|
| On-Premise | NTLM authentication |
| 365 | Basic authentication |

### Connections

Multiple connections are supported. You can easily update your `dynamics` configuration to add as many connections as
you wish.

```php
// Will use the default connection.
Customer::query()->first();

// Uses the supplied connection.
Customer::query('other_connection')->first();
```

## Adding web services

Adding a web service to your configuration is easily done. Start by creating your own resource class to map te data to.

```php
use JustBetter\DynamicsClient\OData\BaseResource;

class Customer extends BaseResource
{
//
}
```

### Primary Key

By default, the primary key of a resource will default to `No` as a string. You can override this by supplying the
variable `$primaryKey`.

```php
public array $primaryKey = [
'Code',
];
```

### Data Casting

Fields in resources will by default be treated as a string. For some fields, like a line number, this should be casted
to an integer.

```php
public array $casts = [
'Line_No' => 'int',
];
```

### Registering Your Resource

Lastly, you should register your resource in your configuration file to let the package know where the web service is
located. This should correspond to the service name configured in Dynamics.

If your resource class name is the same as the service name, no manual configuration is needed.

> **Note:** Make sure your web service is published.
```php
return [

/* Resource Configuration */
'resources' => [
Customer::class => 'CustomerCard',
],

];
```

## Query Builder

Querying data is easily done using the QueryBuilder.

Using the `get` method will only return the first result page. If you wish to efficiently loop through all records,
use `lazy` instead.

```php
$customers = Customer::query()
->where('City', '=', 'Alkmaar')
->lazy()
->each(function(Customer $customer): void {
//
});
```

See the `QueryBuilder` class for all available methods.

## Creating records

Create a new record.

```php
Customer::new()->create([
'Name' => 'John Doe'
])
```

## Updating records

Update an existing record.

```php
$customer = Customer::query()->find('1000');
$customer->update([
'Name' => 'John Doe',
]);
```

## Deleting records

Delete a record.

```php
$customer = Customer::query()->find('1000');
$customer->delete();
```

## Debugging

If you wish to review your query before you sent it, you may want to use the `dd` function on the builder.

```php
Customer::query()
->where('City', '=', 'Alkmaar')
->whereIn('No', ['1000', '2000'])
->dd();

// Customer?$filter=City eq 'Alkmaar' and (No eq '1000' or No eq '2000')
```

## Commands

You can run the following command to check if you can successfully connect to Dynamics.

```shell
php artisan dynamics:connect {connection?}
```

## Contributing

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [Vincent Boon](https://github.com/VincentBean)
- [Ramon Rietdijk](https://github.com/ramonrietdijk)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
52 changes: 52 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "just-better/laravel-dynamics-client",
"description": "A client to connect with Microsoft Dynamics",
"type": "package",
"authors": [
{
"name": "Vincent Boon",
"email": "vincent@justbetter.nl",
"role": "Developer"
},
{
"name": "Ramon Rietdijk",
"email": "ramon@justbetter.nl",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"justbetter/odata-client": "^1.0",
"laravel/framework": "^9.0"
},
"require-dev": {
"laravel/pint": "^1.1",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
"JustBetter\\DynamicsClient\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"JustBetter\\DynamicsClient\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"JustBetter\\DynamicsClient\\ServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading

0 comments on commit f5dbac9

Please sign in to comment.