ContactService   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 120
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createProcess() 0 4 1
A updateProcess() 0 4 1
A deleteProcess() 0 4 1
A createFromArray() 0 11 1
A updateFromArray() 0 11 1
A deleteFromId() 0 11 2
A setDefault() 0 13 3
B hydrateObjectArray() 0 28 7
A resetDefault() 0 13 3
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\DealerContactEvent;
17
use Dealer\Event\DealerEvents;
18
use Dealer\Model\Base\DealerContactQuery;
19
use Dealer\Model\DealerContact;
20
use Dealer\Service\Base\AbstractBaseService;
21
use Dealer\Service\Base\BaseServiceInterface;
22
use Symfony\Component\EventDispatcher\Event;
23
24
/**
25
 * Class ContactService
26
 * @package Dealer\Service
27
 */
28
class ContactService extends AbstractBaseService implements BaseServiceInterface
29
{
30
    const EVENT_CREATE = DealerEvents::DEALER_CONTACT_CREATE;
31
    const EVENT_CREATE_BEFORE = DealerEvents::DEALER_CONTACT_CREATE_BEFORE;
32
    const EVENT_CREATE_AFTER = DealerEvents::DEALER_CONTACT_CREATE_AFTER;
33
    const EVENT_DELETE = DealerEvents::DEALER_CONTACT_DELETE;
34
    const EVENT_DELETE_BEFORE = DealerEvents::DEALER_CONTACT_DELETE_BEFORE;
35
    const EVENT_DELETE_AFTER = DealerEvents::DEALER_CONTACT_DELETE_AFTER;
36
    const EVENT_UPDATE = DealerEvents::DEALER_CONTACT_UPDATE;
37
    const EVENT_UPDATE_BEFORE = DealerEvents::DEALER_CONTACT_UPDATE_BEFORE;
38
    const EVENT_UPDATE_AFTER = DealerEvents::DEALER_CONTACT_UPDATE_AFTER;
39
40
    protected function createProcess(Event $event)
41
    {
42
        $event->getDealerContact()->save();
43
    }
44
45
    protected function updateProcess(Event $event)
46
    {
47
        $event->getDealerContact()->save();
48
    }
49
50
    protected function deleteProcess(Event $event)
51
    {
52
        $event->getDealerContact()->delete();
53
    }
54
55
    public function createFromArray($data, $locale = null)
56
    {
57
        $dealer_contact = $this->hydrateObjectArray($data, $locale);
58
59
        $event = new DealerContactEvent();
60
        $event->setDealerContact($dealer_contact);
61
62
        $this->create($event);
63
64
        return $event->getDealerContact();
65
    }
66
67
    public function updateFromArray($data, $locale = null)
68
    {
69
        $dealer_contact = $this->hydrateObjectArray($data, $locale);
70
71
        $event = new DealerContactEvent();
72
        $event->setDealerContact($dealer_contact);
73
74
        $this->update($event);
75
76
        return $event->getDealerContact();
77
    }
78
79
    public function deleteFromId($id)
80
    {
81
        $dealer = DealerContactQuery::create()->findOneById($id);
82
83
        if ($dealer) {
84
            $event = new DealerContactEvent();
85
            $event->setDealerContact($dealer);
86
87
            $this->delete($event);
88
        }
89
    }
90
91
    public function setDefault($data)
92
    {
93
        if (isset($data['is_default']) && $data['is_default']) {
94
            $this->resetDefault($data);
95
        }
96
97
        $dealer_contact = $this->hydrateObjectArray($data);
98
99
        $event = new DealerContactEvent();
100
        $event->setDealerContact($dealer_contact);
101
102
        $this->update($event);
103
    }
104
105
    protected function hydrateObjectArray($data, $locale = null)
106
    {
107
        $model = new DealerContact();
108
109
        if (isset($data['id'])) {
110
            $dealer = DealerContactQuery::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['label'])) {
122
            $model->setLabel($data['label']);
123
        }
124
        if (isset($data['dealer_id'])) {
125
            $model->setDealerId($data['dealer_id']);
126
        }
127
        if (isset($data['is_default'])) {
128
            $model->setIsDefault($data['is_default']);
129
        }
130
131
        return $model;
132
    }
133
134
    protected function resetDefault($data)
135
    {
136
        if (isset($data['id'])) {
137
            $dealer = DealerContactQuery::create()->findOneById($data['id']);
138
            $defaultsContacts = DealerContactQuery::create()->filterByDealerId($dealer->getDealerId())->filterByIsDefault(true)->find();
139
140
            foreach ($defaultsContacts as $defaultsContact) {
141
                $defaultsContact
142
                    ->setIsDefault(false)
143
                    ->save();
144
            }
145
        }
146
    }
147
}
148