Completed
Push — master ( 700e42...8caa47 )
by Gilmar
39:30 queued 14:31
created

Item::toPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 * For more information, see <http://www.g1mr.com/>.
9
 */
10
11
namespace Gpupo\NetshoesSdk\Entity\Product\Sku;
12
13
use Gpupo\CommonSdk\Entity\EntityAbstract;
14
use Gpupo\CommonSdk\Entity\EntityInterface;
15
16
class Item extends EntityAbstract implements EntityInterface
17
{
18
    protected $primaryKey = 'sku';
19
20
    protected $exclude = ['price', 'priceSchedule', 'stock', 'status'];
21
22 7
    public function getSchema()
23
    {
24
        return  [
25 7
            'sku'           => 'string',
26
            'name'          => 'string',
27
            'description'   => 'string',
28
            'color'         => 'string',
29
            'size'          => 'string',
30
            'gender'        => 'string',
31
            'eanIsbn'       => 'string',
32
            'images'        => 'object',
33
            'video'         => 'string',
34
            'height'        => 'string',
35
            'width'         => 'string',
36
            'depth'         => 'string',
37
            'weight'        => 'string',
38
            'price'         => 'object',
39
            'priceSchedule' => 'object',
40
            'stock'         => 'object',
41
            'status'        => 'object',
42
        ];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 7
    protected function setUp()
49
    {
50 7
        $this->setOptionalSchema($this->exclude);
51 7
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 7
    protected function beforeConstruct($data = null)
57
    {
58 7
        if (empty($data)) {
59
            return;
60
        }
61
62 7
        if (is_array($data)) {
63 7
            if (array_key_exists('stock', $data)) {
64
                $data['stock'] = ['available' => $data['stock']];
65
            }
66
67 7
            if (array_key_exists('listPrice', $data)) {
68
                $data['price'] = ['price' => $data['listPrice']];
69
                unset($data['listPrice']);
70
            }
71
72 7
            if (array_key_exists('sellPrice', $data)) {
73
                $data['priceSchedule'] = ['priceTo' => $data['sellPrice']];
74
                unset($data['sellPrice']);
75
            }
76
77 7
            if (array_key_exists('status', $data)) {
78
                $data['status'] = ['active' => $data['status']];
79
            }
80
        }
81
82 7
        return $data;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    public function toArray()
89
    {
90 2
        $a = parent::toArray();
91
92 2
        foreach ($this->exclude as $k) {
93 2
            unset($a[$k]);
94
        }
95
96 2
        return $a;
97
    }
98
99
    public function toPrice()
100
    {
101
        return $this->getPrice()->toJson();
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...
102
    }
103
}
104