Completed
Push — master ( d15f5b...9b96d8 )
by Gilmar
26:33
created

Item::beforeConstruct()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
ccs 14
cts 15
cp 0.9333
rs 6.7272
cc 7
eloc 15
nc 18
nop 1
crap 7.0145
1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the information of copyright and license you should read the file
7
 * LICENSE which is distributed with this source code.
8
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
9
 * LICENSE que é distribuído com este código-fonte.
10
 * Para obtener la información de los derechos de autor y la licencia debe leer
11
 * el archivo LICENSE que se distribuye con el código fuente.
12
 * For more information, see <http://www.g1mr.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Entity\Product\Sku;
16
17
use Gpupo\CommonSdk\Entity\EntityAbstract;
18
use Gpupo\CommonSdk\Entity\EntityInterface;
19
20
final class Item extends EntityAbstract implements EntityInterface
21
{
22
    protected $primaryKey = 'sku';
23
24
    protected $exclude = ['price', 'priceSchedule', 'stock', 'status'];
25
26
    /**
27
     * @codeCoverageIgnore
28
     */
29
    public function getSchema()
30
    {
31
        return  [
32
            'sku'           => 'string',
33
            'name'          => 'string',
34
            'description'   => 'string',
35
            'color'         => 'string',
36
            'size'          => 'string',
37
            'gender'        => 'string',
38
            'eanIsbn'       => 'string',
39
            'images'        => 'object',
40
            'video'         => 'string',
41
            'height'        => 'string',
42
            'width'         => 'string',
43
            'depth'         => 'string',
44
            'weight'        => 'string',
45
            'price'         => 'object',
46
            'priceSchedule' => 'object',
47
            'stock'         => 'object',
48
            'status'        => 'object',
49
        ];
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 109
    protected function setUp()
56
    {
57 109
        $this->setOptionalSchema($this->exclude);
58 109
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 113
    protected function beforeConstruct($data = null)
64
    {
65 113
        if (empty($data)) {
66
            return;
67
        }
68
69 113
        if (is_array($data)) {
70 113
            if (array_key_exists('stock', $data)) {
71 106
                $data['stock'] = ['available' => $data['stock']];
72
            }
73
74 113
            if (array_key_exists('listPrice', $data)) {
75 106
                $data['price'] = ['price' => $data['listPrice']];
76 106
                unset($data['listPrice']);
77
            }
78
79 113
            if (array_key_exists('sellPrice', $data)) {
80 106
                $data['priceSchedule'] = ['priceTo' => $data['sellPrice']];
81 106
                unset($data['sellPrice']);
82
            }
83
84 113
            if (array_key_exists('status', $data)) {
85 106
                $data['status'] = ['active' => $data['status']];
86
            }
87
        }
88
89 113
        return $data;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 8
    public function toArray()
96
    {
97 8
        $a = parent::toArray();
98
99 8
        foreach ($this->exclude as $k) {
100 8
            unset($a[$k]);
101
        }
102
103 8
        return $a;
104
    }
105
106 1
    protected function toPrice()
107
    {
108 1
        return $this->getPrice()->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method getPrice does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109
    }
110
111 1
    protected function toPriceSchedule()
112
    {
113 1
        return $this->getPriceSchedule()->toArray();
0 ignored issues
show
Bug introduced by
The method getPriceSchedule() does not exist on Gpupo\NetshoesSdk\Entity\Product\Sku\Item. Did you maybe mean toPriceSchedule()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
    }
115
116 1
    protected function toStatus()
117
    {
118 1
        return $this->getStatus()->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method getStatus does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
    }
120
121 1
    protected function toStock()
122
    {
123 1
        return $this->getStock()->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method getStock does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
124
    }
125
}
126