Skip to content

Commit ee7fd8f

Browse files
authored
Merge pull request #2 from kg-bot/customer_addresses
Customer addresses
2 parents 176de41 + dec453a commit ee7fd8f

File tree

5 files changed

+124
-61
lines changed

5 files changed

+124
-61
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": ">=7.1",
14-
"laravel/framework": "^5.5|^6|^7",
14+
"laravel/framework": "^5.5|^6.20.12|^7.30.3",
1515
"ext-json": "*",
1616
"guzzlehttp/guzzle": "*"
1717
},
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace KgBot\Magento\Builders;
4+
5+
6+
use KgBot\Magento\Exceptions\MagentoMethodNotImplementedException;
7+
use KgBot\Magento\Models\CustomerAddress;
8+
9+
class CustomerAddressBuilder extends Builder
10+
{
11+
protected $entity = 'customers/addresses';
12+
protected $model = CustomerAddress::class;
13+
14+
public function get( $filters = [] ) {
15+
throw new MagentoMethodNotImplementedException( 'This method is not available on this resource' );
16+
}
17+
18+
public function create( $data ) {
19+
throw new MagentoMethodNotImplementedException( 'This method is not available on this resource' );
20+
}
21+
}

src/Magento.php

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: nts
5-
* Date: 31.3.18.
6-
* Time: 15.12
7-
*/
82

93
namespace KgBot\Magento;
104

5+
use KgBot\Magento\Builders\CustomerAddressBuilder;
116
use KgBot\Magento\Builders\CustomerBuilder;
127
use KgBot\Magento\Builders\CustomerGroupBuilder;
138
use KgBot\Magento\Builders\OrderBuilder;
@@ -16,63 +11,64 @@
1611

1712
class Magento
1813
{
19-
/**
20-
* @var $request Request
21-
*/
22-
protected $request;
14+
/**
15+
* @var $request Request
16+
*/
17+
protected $request;
2318

24-
/**
25-
* Rackbeat constructor.
26-
*
27-
* @param null $token API token
28-
* @param array $options Custom Guzzle options
29-
* @param array $headers Custom Guzzle headers
30-
*/
31-
public function __construct( $token = null, $options = [], $headers = [] )
32-
{
33-
$this->initRequest( $token, $options, $headers );
34-
}
19+
/**
20+
* Rackbeat constructor.
21+
*
22+
* @param null $token API token
23+
* @param array $options Custom Guzzle options
24+
* @param array $headers Custom Guzzle headers
25+
*/
26+
public function __construct( $token = null, $options = [], $headers = [] ) {
27+
$this->initRequest( $token, $options, $headers );
28+
}
3529

36-
/**
37-
* @param $token
38-
* @param array $options
39-
* @param array $headers
40-
*/
41-
private function initRequest( $token, $options = [], $headers = [] )
42-
{
43-
$this->request = new Request( $token, $options, $headers );
44-
}
30+
/**
31+
* @param $token
32+
* @param array $options
33+
* @param array $headers
34+
*/
35+
private function initRequest( $token, $options = [], $headers = [] ): void {
36+
$this->request = new Request( $token, $options, $headers );
37+
}
4538

46-
/**
47-
* @return \KgBot\Magento\Builders\OrderBuilder
48-
*/
49-
public function orders()
50-
{
51-
return new OrderBuilder( $this->request );
52-
}
39+
/**
40+
* @return OrderBuilder
41+
*/
42+
public function orders(): OrderBuilder {
43+
return new OrderBuilder( $this->request );
44+
}
5345

54-
/**
55-
* @return \KgBot\Magento\Builders\CustomerBuilder
56-
*/
57-
public function customers()
58-
{
59-
return new CustomerBuilder( $this->request );
60-
}
46+
/**
47+
* @return CustomerBuilder
48+
*/
49+
public function customers(): CustomerBuilder {
50+
return new CustomerBuilder( $this->request );
51+
}
6152

62-
/**
63-
* @return \KgBot\Magento\Builders\ProductBuilder
64-
*/
65-
public function products()
66-
{
67-
return new ProductBuilder( $this->request );
68-
}
53+
/**
54+
* @return CustomerAddressBuilder
55+
*/
56+
public function customer_addresses(): CustomerAddressBuilder {
57+
return new CustomerAddressBuilder( $this->request );
58+
}
6959

70-
/**
71-
* @return \KgBot\Magento\Builders\CustomerGroupBuilder
72-
*/
73-
public function customer_groups()
74-
{
60+
/**
61+
* @return ProductBuilder
62+
*/
63+
public function products(): ProductBuilder {
64+
return new ProductBuilder( $this->request );
65+
}
7566

76-
return new CustomerGroupBuilder( $this->request );
77-
}
67+
/**
68+
* @return CustomerGroupBuilder
69+
*/
70+
public function customer_groups(): CustomerGroupBuilder {
71+
72+
return new CustomerGroupBuilder( $this->request );
73+
}
7874
}

src/Models/Customer.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313

1414
class Customer extends Model
1515
{
16-
protected $entity = 'customers';
17-
protected $primaryKey = 'id';
16+
protected $entity = 'customers';
17+
protected $primaryKey = 'id';
18+
19+
public function getBillingAddress() {
20+
return $this->request->handleWithExceptions( function () {
21+
22+
$address = $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}/billingAddress" );
23+
$responseData = json_decode( (string) $address->getBody() );
24+
25+
if ( ! empty( $responseData ) ) {
26+
return new CustomerAddress( $this->request, $responseData );
27+
}
28+
29+
return null;
30+
} );
31+
}
32+
33+
public function getShippingAddress() {
34+
return $this->request->handleWithExceptions( function () {
35+
36+
$address = $this->request->client->get( "{$this->entity}/{$this->{$this->primaryKey}}/shippingAddress" );
37+
$responseData = json_decode( (string) $address->getBody() );
38+
39+
if ( ! empty( $responseData ) ) {
40+
return new CustomerAddress( $this->request, $responseData );
41+
}
42+
43+
return null;
44+
} );
45+
}
1846
}

src/Models/CustomerAddress.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: nts
5+
* Date: 19.4.18.
6+
* Time: 01.30
7+
*/
8+
9+
namespace KgBot\Magento\Models;
10+
11+
12+
use KgBot\Magento\Utils\Model;
13+
14+
class CustomerAddress extends Model
15+
{
16+
protected $entity = 'customers/addresses';
17+
protected $primaryKey = 'id';
18+
}

0 commit comments

Comments
 (0)