Completed
Push — master ( c7990b...4ae46d )
by Gilmar
24:18
created

CommonSchema::callOrder()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 8.5906
cc 5
eloc 14
nc 6
nop 1
crap 30
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\Order\Decorator;
16
17
use Gpupo\CommonSchema\Trading\OrderSchema;
18
use Gpupo\CommonSchema\Trading\OrderTrait;
19
20
class CommonSchema extends AbstractDecorator implements DecoratorInterface
21
{
22
    use OrderTrait;
23
24
    protected function getAcceptedOffer()
25
    {
26
        return $this->getOrder()->getItems()->toSchema();
27
    }
28
29
    protected function getCustomer()
30
    {
31
        return $this->getOrder()->getShipping()->getCustomer()->toSchema();
32
    }
33
34
    protected function getBillingAddress()
35
    {
36
        return $this->getOrder()->getShipping()->getCustomer()->getAddress()->toSchema();
37
    }
38
39
    protected function callOrder($key)
40
    {
41
        $dict = [
42
            'merchant' => 'originSite',
43
            'price'    => 'totalNet',
44
            'discount' => 'totalDiscount',
45
        ];
46
47
        if (array_key_exists($key, $dict) && !is_array($dict[$key])) {
48
            $key = $dict[$key];
49
        }
50
        $method = 'get'.ucfirst($key);
51
52
        if (method_exists(__CLASS__, $method)) {
53
            return $this->$method();
54
        }
55
56
        try {
57
            return $this->getOrder()->$method();
58
        } catch (\BadMethodCallException $e) {
59
            error_log('protected function '.$method.'(){}');
60
        }
61
    }
62
63 1
    public function toArray()
64
    {
65 1
        $schema = OrderSchema::getInstance()->getSchema();
66 1
        $output = [];
67 1
        foreach ($schema as $k => $v) {
68 1
            $output[$k] = $this->callOrder($k);
69
        }
70 1
        OrderSchema::getInstance()->validate($output);
71
72 1
        return $output;
73
    }
74
}
75