DealerService::hydrateObjectArray()   F
last analyzed

Complexity

Conditions 16
Paths > 20000

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 1.4
c 0
b 0
f 0
cc 16
nc 24576
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Service;
15
16
use Dealer\Event\DealerEvent;
17
use Dealer\Event\DealerEvents;
18
use Dealer\Model\Dealer;
19
use Dealer\Model\DealerQuery;
20
use Dealer\Service\Base\AbstractBaseService;
21
use Dealer\Service\Base\BaseServiceInterface;
22
use Symfony\Component\EventDispatcher\Event;
23
24
/**
25
 * Class DealerService
26
 * @package Dealer\Service
27
 */
28
class DealerService extends AbstractBaseService implements BaseServiceInterface
29
{
30
    const EVENT_CREATE = DealerEvents::DEALER_CREATE;
31
    const EVENT_CREATE_BEFORE = DealerEvents::DEALER_CREATE_BEFORE;
32
    const EVENT_CREATE_AFTER = DealerEvents::DEALER_CREATE_AFTER;
33
    const EVENT_DELETE = DealerEvents::DEALER_DELETE;
34
    const EVENT_DELETE_BEFORE = DealerEvents::DEALER_DELETE_BEFORE;
35
    const EVENT_DELETE_AFTER = DealerEvents::DEALER_DELETE_AFTER;
36
    const EVENT_UPDATE = DealerEvents::DEALER_UPDATE;
37
    const EVENT_UPDATE_BEFORE = DealerEvents::DEALER_UPDATE_BEFORE;
38
    const EVENT_UPDATE_AFTER = DealerEvents::DEALER_UPDATE_AFTER;
39
40
    protected function createProcess(Event $event)
41
    {
42
        $event->getDealer()->save();
43
    }
44
45
    protected function updateProcess(Event $event)
46
    {
47
        $event->getDealer()->save();
48
    }
49
50
    protected function deleteProcess(Event $event)
51
    {
52
        $event->getDealer()->delete();
53
    }
54
55
    public function createFromArray($data, $locale = null)
56
    {
57
        $dealer = $this->hydrateObjectArray($data, $locale);
58
59
        $event = new DealerEvent();
60
        $event->setDealer($dealer);
61
62
        $this->create($event);
63
64
        return $event->getDealer();
65
    }
66
67
    public function updateFromArray($data, $locale = null)
68
    {
69
        $dealer = $this->hydrateObjectArray($data, $locale);
70
71
        $event = new DealerEvent();
72
        $event->setDealer($dealer);
73
74
        $this->update($event);
75
76
        return $event->getDealer();
77
    }
78
79
    public function deleteFromId($id)
80
    {
81
        $dealer = DealerQuery::create()->findOneById($id);
82
        if ($dealer) {
83
            $event = new DealerEvent();
84
            $event->setDealer($dealer);
85
86
            $this->delete($event);
87
        }
88
    }
89
90
91
    public function toggleVisibilityFromId($id)
92
    {
93
        $dealer = DealerQuery::create()->findOneById($id);
94
95
        $dealer->setVisible($dealer->getVisible() ? 0 : 1);
96
97
        $event = new DealerEvent();
98
        $event->setDealer($dealer);
99
100
        $this->update($event);
101
102
        return $event->getDealer();
103
    }
104
105
    protected function hydrateObjectArray($data, $locale = null)
106
    {
107
        $model = new Dealer();
108
109
        if (isset($data['id'])) {
110
            $dealer = DealerQuery::create()->findOneById($data['id']);
111
            if ($dealer) {
112
                $model = $dealer;
113
            }
114
        }
115
116
        if ($locale) {
117
            $model->setLocale($locale);
118
        }
119
120
        // Require Field
121
        if (isset($data['title'])) {
122
            $model->setTitle($data['title']);
123
        }
124
        if (isset($data['address1'])) {
125
            $model->setAddress1($data['address1']);
126
        }
127
        if (isset($data['zipcode'])) {
128
            $model->setZipcode($data['zipcode']);
129
        }
130
        if (isset($data['city'])) {
131
            $model->setCity($data['city']);
132
        }
133
        if (isset($data['country_id'])) {
134
            $model->setCountryId($data['country_id']);
135
        }
136
        if (isset($data['visible'])) {
137
            $model->setVisible($data['visible']);
138
        }
139
140
        //  Optionnal Field
141
        if (isset($data['description'])) {
142
            $model->setDescription($data['description']);
143
        } else {
144
            $model->setDescription(null);
145
        }
146
147
        //  Optionnal Field
148
        if (isset($data['big_description'])) {
149
            $model->setBigDescription($data['big_description']);
150
        } else {
151
            $model->setBigDescription(null);
152
        }
153
154
        //  Optionnal Field
155
        if (isset($data['hard_open_hour'])) {
156
            $model->setHardOpenHour($data['hard_open_hour']);
157
        } else {
158
            $model->setHardOpenHour(null);
159
        }
160
161
        if (isset($data['access'])) {
162
            $model->setAccess($data['access']);
163
        } else {
164
            $model->setAccess(null);
165
        }
166
167
        if (isset($data['address2'])) {
168
            $model->setAddress2($data['address2']);
169
        } else {
170
            $model->setAddress2(null);
171
        }
172
173
        if (isset($data['address3'])) {
174
            $model->setAddress3($data['address3']);
175
        } else {
176
            $model->setAddress3(null);
177
        }
178
179
        return $model;
180
    }
181
}
182