Completed
Push — master ( 798567...9a7817 )
by Gilmar
104:38 queued 79:41
created

Item::beforeConstruct()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.9809

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
ccs 8
cts 15
cp 0.5333
rs 6.7272
cc 7
eloc 15
nc 18
nop 1
crap 11.9809
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
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 4
    protected function setUp()
56
    {
57 4
        $this->setOptionalSchema($this->exclude);
58 4
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    protected function beforeConstruct($data = null)
64
    {
65 4
        if (empty($data)) {
66
            return;
67
        }
68
69 4
        if (is_array($data)) {
70 4
            if (array_key_exists('stock', $data)) {
71
                $data['stock'] = ['available' => $data['stock']];
72
            }
73
74 4
            if (array_key_exists('listPrice', $data)) {
75
                $data['price'] = ['price' => $data['listPrice']];
76
                unset($data['listPrice']);
77
            }
78
79 4
            if (array_key_exists('sellPrice', $data)) {
80
                $data['priceSchedule'] = ['priceTo' => $data['sellPrice']];
81
                unset($data['sellPrice']);
82
            }
83
84 4
            if (array_key_exists('status', $data)) {
85
                $data['status'] = ['active' => $data['status']];
86
            }
87
        }
88
89 4
        return $data;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function toArray()
96
    {
97 2
        $a = parent::toArray();
98
99 2
        foreach ($this->exclude as $k) {
100 2
            unset($a[$k]);
101
        }
102
103 2
        return $a;
104
    }
105
106
    protected function toPrice()
107
    {
108
        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
    protected function toPriceSchedule()
112
    {
113
        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
    protected function toStatus()
117
    {
118
        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
    protected function toStock()
122
    {
123
        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