Completed
Push — master ( 4d6a24...d6fdf9 )
by Antony
03:37
created

ContactService::updateProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
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_AFTER;
32
    const EVENT_CREATE_AFTER = DealerEvents::DEALER_CONTACT_CREATE_BEFORE;
33
    const EVENT_DELETE = DealerEvents::DEALER_CONTACT_DELETE;
34
    const EVENT_DELETE_BEFORE = DealerEvents::DEALER_CONTACT_DELETE_AFTER;
35
    const EVENT_DELETE_AFTER = DealerEvents::DEALER_CONTACT_DELETE_BEFORE;
36
    const EVENT_UPDATE = DealerEvents::DEALER_CONTACT_UPDATE;
37
    const EVENT_UPDATE_BEFORE = DealerEvents::DEALER_CONTACT_UPDATE_AFTER;
38
    const EVENT_UPDATE_AFTER = DealerEvents::DEALER_CONTACT_UPDATE_BEFORE;
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 = $this->hydrateObjectArray(['id' => $id]);
82
83
        $event = new DealerContactEvent();
84
        $event->setDealerContact($dealer);
85
86
        $this->delete($event);
87
    }
88
89
    public function setDefault($data)
90
    {
91
        if (isset($data['is_default']) && $data['is_default']) {
92
            $this->resetDefault($data);
93
        }
94
95
        $dealer_contact = $this->hydrateObjectArray($data);
96
97
        $event = new DealerContactEvent();
98
        $event->setDealerContact($dealer_contact);
99
100
        $this->update($event);
101
    }
102
103
    protected function hydrateObjectArray($data, $locale = null)
104
    {
105
        $model = new DealerContact();
106
107
        if (isset($data['id'])) {
108
            $dealer = DealerContactQuery::create()->findOneById($data['id']);
109
            if ($dealer) {
110
                $model = $dealer;
111
            }
112
        }
113
114
        if ($locale) {
115
            $model->setLocale($locale);
116
        }
117
118
        // Require Field
119
        if (isset($data['label'])) {
120
            $model->setLabel($data['label']);
121
        }
122
        if (isset($data['dealer_id'])) {
123
            $model->setDealerId($data['dealer_id']);
124
        }
125
        if (isset($data['is_default'])) {
126
            $model->setIsDefault($data['is_default']);
127
        }
128
129
        return $model;
130
    }
131
132
    protected function resetDefault($data)
133
    {
134
135
        if (isset($data['id'])) {
136
            $dealer = DealerContactQuery::create()->findOneById($data['id']);
137
            $defaultsContacts = DealerContactQuery::create()->filterByDealerId($dealer->getDealerId())->filterByIsDefault(true)->find();
138
139
            foreach ($defaultsContacts as $defaultsContact) {
140
                $defaultsContact
141
                    ->setIsDefault(false)
142
                    ->save();
143
            }
144
        }
145
146
    }
147
}