Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRafn committed Dec 5, 2016
0 parents commit bff85ec
Show file tree
Hide file tree
Showing 22 changed files with 623 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2016 Lasse Rafn <lasserafn@gmail.com>

> 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.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "lasserafn/laravel-economic",
"description": "Economic REST wrapper for Laravel",
"keywords": ["laravel", "wrapper", "economic", "api", "integration", "e-conomic"],
"license": "MIT",
"authors": [
{
"name": "Lasse Rafn",
"email": "lasserafn@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.1.*|5.2.*|5.3.*",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"LasseRafn\\Economic\\": "src"
}
}
}
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Laravel Economic REST wrapper

#Installation

1. Require using composer
````
composer require lasserafn/laravel-economic
````

2. Add the EconomicServiceProvider to your ````config/app.php```` providers array.
````
'providers' => [
\LasseRafn\Economic\EconomicServiceProvider::class,
]
````
123 changes: 123 additions & 0 deletions src/Builders/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Utils\Model;
use LasseRafn\Economic\Utils\Request;

class Builder
{
private $request;
protected $entity;

/** @var Model */
protected $model;

function __construct( Request $request )
{
$this->request = $request;
}

/**
* @param $id
*
* @return mixed|Model
*/
public function find($id)
{//todo test
$response = $this->request->curl->get( "/{$this->entity}/{$id}" );

// todo check for errors and such

$responseData = json_decode( $response->getBody()->getContents() );

return new $this->model($this->request, $responseData);
}

public function first()
{
$response = $this->request->curl->get( "/{$this->entity}?skippages=0&pagesize=1" );

// todo check for errors and such

$responseData = json_decode( $response->getBody()->getContents() );
$fetchedItems = $responseData->collection;

return new $this->model($this->request, $fetchedItems[0]);
}

/**
* @return \Illuminate\Support\Collection|Model[]
*/
public function get()
{
$response = $this->request->curl->get( "/{$this->entity}" );

// todo check for errors and such

$responseData = json_decode( $response->getBody()->getContents() );
$fetchedItems = $responseData->collection;

$items = collect( [] );
foreach ( $fetchedItems as $item )
{
/** @var Model $model */
$model = new $this->model( $this->request, $item );

$items->push( $model );
}

return $items;
}

/**
* @return \Illuminate\Support\Collection|Model[]
*/
public function all()
{
$page = 0;
$pagesize = 500; // Yes, we could move this to 1000, but honestly I'd rather send two requests than stall their servers.
$hasMore = true;
$items = collect( [] );

while($hasMore)
{
$response = $this->request->curl->get( "/{$this->entity}?skippages={$page}&pagesize={$pagesize}" );

// todo check for errors and such

$responseData = json_decode( $response->getBody()->getContents() );
$fetchedItems = $responseData->collection;

if( count($fetchedItems) == 0)
{
$hasMore = false;

break;
}

foreach ( $fetchedItems as $item )
{
/** @var Model $model */
$model = new $this->model( $this->request, $item );

$items->push( $model );
}

$page++;
}

return $items;
}

public function create( $data)
{
$response = $this->request->curl->post( "/{$this->entity}", [
'json' => $data
] );

$responseData = json_decode( $response->getBody()->getContents() );

return new $this->model( $this->request, $responseData );
}


}
17 changes: 17 additions & 0 deletions src/Builders/ContactBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\Contact;
use LasseRafn\Economic\Utils\Request;

class ContactBuilder extends Builder
{
protected $entity = 'customers/:customerNumber/contacts';
protected $model = Contact::class;

public function __construct( Request $request, $customerNumber )
{
$this->entity = str_replace(':customerNumber', $customerNumber, $this->entity);

parent::__construct( $request );
}
}
9 changes: 9 additions & 0 deletions src/Builders/CustomerBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\Customer;

class CustomerBuilder extends Builder
{
protected $entity = 'customers';
protected $model = Customer::class;
}
9 changes: 9 additions & 0 deletions src/Builders/CustomerGroupBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\CustomerGroup;

class CustomerGroupBuilder extends Builder
{
protected $entity = 'customer-groups';
protected $model = CustomerGroup::class;
}
9 changes: 9 additions & 0 deletions src/Builders/LayoutBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\Layout;

class LayoutBuilder extends Builder
{
protected $entity = 'layouts';
protected $model = Layout::class;
}
9 changes: 9 additions & 0 deletions src/Builders/PaymentTermBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\PaymentTerm;

class PaymentTermBuilder extends Builder
{
protected $entity = 'payment-terms';
protected $model = PaymentTerm::class;
}
9 changes: 9 additions & 0 deletions src/Builders/VatZoneBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace LasseRafn\Economic\Builders;

use LasseRafn\Economic\Models\VatZone;

class VatZoneBuilder extends Builder
{
protected $entity = 'vat-zones';
protected $model = VatZone::class;
}
64 changes: 64 additions & 0 deletions src/Economic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace LasseRafn\Economic;

use LasseRafn\Economic\Builders\Builder;
use LasseRafn\Economic\Builders\CustomerBuilder;
use LasseRafn\Economic\Builders\CustomerGroupBuilder;
use LasseRafn\Economic\Builders\LayoutBuilder;
use LasseRafn\Economic\Builders\PaymentTermBuilder;
use LasseRafn\Economic\Builders\VatZoneBuilder;
use LasseRafn\Economic\Utils\Request;

class Economic
{
protected $request;

public function __construct($agreement = '')
{
$this->request = new Request($agreement);
}

public function getAuthUrl()
{
return config('economic.auth_endpoint') . config('economic.public_token');
}

/**
* @return CustomerBuilder|Builder
*/
public function customers()
{
return new CustomerBuilder($this->request);
}

/**
* @return CustomerGroupBuilder|Builder
*/
public function customersGroups()
{
return new CustomerGroupBuilder($this->request);
}

/**
* @return LayoutBuilder|Builder
*/
public function layouts()
{
return new LayoutBuilder($this->request);
}

/**
* @return VatZoneBuilder|Builder
*/
public function vatZones()
{
return new VatZoneBuilder($this->request);
}

/**
* @return VatZoneBuilder|Builder
*/
public function paymentTerms()
{
return new PaymentTermBuilder($this->request);
}
}
25 changes: 25 additions & 0 deletions src/EconomicServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php namespace LasseRafn\Economic;

use Illuminate\Support\ServiceProvider;

class EconomicServiceProvider extends ServiceProvider
{
/**
* Boot
*/
public function boot()
{
$configPath = __DIR__ . '/config/economic.php';
$this->mergeConfigFrom($configPath, 'economic');

$configPath = __DIR__ . '/config/economic.php';

if (function_exists('config_path')) {
$publishPath = config_path('economic.php');
} else {
$publishPath = base_path('config/economic.php');
}

$this->publishes([$configPath => $publishPath], 'config');
}
}
35 changes: 35 additions & 0 deletions src/Models/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace LasseRafn\Economic\Models;

use LasseRafn\Economic\Utils\Model;
use LasseRafn\Economic\Utils\Request;

class Contact extends Model
{
protected $entity = 'customers/:customerNumber/contacts';
protected $primaryKey = 'customerContactNumber';
protected $fillable = [
'customerContactNumber',
'self',
'email',
'phone',
'name',
'customer',
];

public $customerContactNumber;
public $self;
public $phone;
public $email;
public $name;

/** @var Customer $customer */
public $customer;

public function __construct( Request $request, array $data )
{
\Log::info($data['customer']);
$this->entity = str_replace(':customerNumber', $data['customer']->customerNumber, $this->entity);

parent::__construct( $request, $data );
}
}
Loading

0 comments on commit bff85ec

Please sign in to comment.