Completed
Push — master ( 9aab81...a1737f )
by Oss
08:58
created

DataModel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 100 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 115
loc 115
rs 10
c 1
b 0
f 0
ccs 30
cts 30
cp 1
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 15 15 3
A getItemList() 3 3 1
A setItemList() 4 4 1
A toArray() 11 11 3
A getRequiredFields() 3 3 1
A getEndpoint() 3 3 1
A addItem() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @category    Brownie/CartsGuru
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\CartsGuru\Model\Base;
9
10
use Brownie\CartsGuru\Exception\ValidateException;
11
use Brownie\CartsGuru\Model\Item;
12
use Brownie\CartsGuru\Model\ItemList;
13
14
/**
15
 * Data model.
16
 *
17
 * @method  DataModel    setSiteId($siteId)    Sets siteId.
18
 */
19 View Code Duplication
abstract class DataModel extends ArrayList
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
22
    /**
23
     * List of required fields.
24
     *
25
     * @var array
26
     */
27
    protected $requiredFields = array();
28
29
    /**
30
     * Endpoint name.
31
     *
32
     * @var string
33
     */
34
    protected $endpoint = '';
35
36
    /**
37
     * Returns the field list as an array.
38
     *
39
     * @return array
40
     */
41 4
    public function toArray()
42
    {
43 4
        $args = parent::toArray();
44 4
        if ($args['items']) {
45 2
            $list = array();
46 2
            foreach ($args['items']->toArray() as $item) {
47 2
                $list[] = array_filter($item->toArray());
48
            }
49 2
            $args['items'] = $list;
50
        }
51 4
        return array_filter($args);
52
    }
53
54
    /**
55
     * Add item.
56
     *
57
     * @param Item  $item   Product.
58
     *
59
     * @return self
60
     */
61 2
    public function addItem(Item $item)
62
    {
63 2
        if (is_null($this->getItemList())) {
64 2
            $this->setItemList(new ItemList());
65
        }
66 2
        $this->getItemList()->add($item);
67 2
        return $this;
68
    }
69
70
    /**
71
     * Set item list.
72
     *
73
     * @param ItemList      $itemList       Item list.
74
     *
75
     * @return self
76
     */
77 2
    private function setItemList(ItemList $itemList)
78
    {
79 2
        $this->fields['items'] = $itemList;
80 2
        return $this;
81
    }
82
83
    /**
84
     * Return item list.
85
     *
86
     * @return ItemList
87
     */
88 2
    private function getItemList()
89
    {
90 2
        return $this->fields['items'];
91
    }
92
93
    /**
94
     * Validates contact data.
95
     *
96
     * @throws ValidateException
97
     */
98 4
    public function validate()
99
    {
100 4
        $args = array_filter($this->toArray());
101
102 4
        $keys = array_diff($this->getRequiredFields(), array_keys($args));
103
104 4
        if (!empty($keys)) {
105 2
            throw new ValidateException('No required fields: ' . implode(', ', $keys));
106
        }
107
108
        /**
109
         * @var $item   Item    Product.
110
         */
111 2
        foreach ($this->getItemList()->toArray() as $item) {
112 2
            $item->validate();
113
        }
114 2
    }
115
116
    /**
117
     * Returns a list of required fields.
118
     *
119
     * @return array
120
     */
121 4
    protected function getRequiredFields()
122
    {
123 4
        return $this->requiredFields;
124
    }
125
126
    /**
127
     * Returns a endpoint name.
128
     *
129
     * @return string
130
     */
131 2
    public function getEndpoint()
132
    {
133 2
        return $this->endpoint;
134
    }
135
}
136