BrandLinkService::createFromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\DealerBrandLinkEvent;
17
use Dealer\Event\DealerEvents;
18
use Dealer\Model\DealerBrand;
19
use Dealer\Model\DealerBrandQuery;
20
use Dealer\Service\Base\AbstractBaseService;
21
use Dealer\Service\Base\BaseServiceInterface;
22
use Symfony\Component\EventDispatcher\Event;
23
24
/**
25
 * Class BrandLinkService
26
 * @package Dealer\Service
27
 */
28
class BrandLinkService extends AbstractBaseService implements BaseServiceInterface
29
{
30
31
    const EVENT_CREATE = DealerEvents::DEALER_BRAND_LINK_CREATE;
32
    const EVENT_CREATE_BEFORE = DealerEvents::DEALER_BRAND_LINK_CREATE_BEFORE;
33
    const EVENT_CREATE_AFTER = DealerEvents::DEALER_BRAND_LINK_CREATE_AFTER;
34
    const EVENT_DELETE = DealerEvents::DEALER_BRAND_LINK_DELETE;
35
    const EVENT_DELETE_BEFORE = DealerEvents::DEALER_BRAND_LINK_DELETE_BEFORE;
36
    const EVENT_DELETE_AFTER = DealerEvents::DEALER_BRAND_LINK_DELETE_AFTER;
37
    const EVENT_UPDATE = DealerEvents::DEALER_BRAND_LINK_UPDATE;
38
    const EVENT_UPDATE_BEFORE = DealerEvents::DEALER_BRAND_LINK_UPDATE_BEFORE;
39
    const EVENT_UPDATE_AFTER = DealerEvents::DEALER_BRAND_LINK_UPDATE_AFTER;
40
41
    /**
42
     * @inheritDoc
43
     */
44
    protected function createProcess(Event $event)
45
    {
46
        /** @var DealerBrandLinkEvent $event */
47
        $event->getDealerBrandLink()->save();
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    protected function updateProcess(Event $event)
54
    {
55
        /** @var DealerBrandLinkEvent $event */
56
        $event->getDealerBrandLink()->save();
57
    }
58
59
    protected function deleteProcess(Event $event)
60
    {
61
        /** @var DealerBrandLinkEvent $event */
62
        $event->getDealerBrandLink()->delete();
63
    }
64
65
    public function createFromArray($data, $locale = null)
66
    {
67
        $link = $this->hydrateObjectArray($data, $locale);
68
69
        $event = new DealerBrandLinkEvent();
70
        $event->setDealerBrandLink($link);
71
72
        $this->create($event);
73
74
        return $event->getDealerBrandLink();
75
    }
76
77
    public function updateFromArray($data, $locale = null)
78
    {
79
        $link = $this->hydrateObjectArray($data, $locale);
80
81
        $event = new DealerBrandLinkEvent();
82
        $event->setDealerBrandLink($link);
83
84
        $this->update($event);
85
86
        return $event->getDealerBrandLink();
87
    }
88
89
    public function deleteFromId($id)
90
    {
91
        $link = DealerBrandQuery::create()->findOneById($id);
92
        if ($link) {
93
            $event = new DealerBrandLinkEvent();
94
            $event->setDealerBrandLink($link);
95
96
            $this->delete($event);
97
        }
98
    }
99
100
    public function deleteFromArray($data)
101
    {
102
        $link = null;
103
104
        if (isset($data["brand_id"]) && isset($data["dealer_id"])) {
105
            $link = DealerBrandQuery::create()->filterByDealerId($data["dealer_id"])->filterByBrandId($data["brand_id"])->findOne();
106
        }
107
108
        if ($link) {
109
            $event = new DealerBrandLinkEvent();
110
            $event->setDealerBrandLink($link);
111
112
            $this->delete($event);
113
        }
114
    }
115
116
    protected function hydrateObjectArray($data, $locale = null)
0 ignored issues
show
Unused Code introduced by
The parameter $locale is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        $model = new DealerBrand();
119
120
        if (isset($data['id'])) {
121
            $link = DealerBrandQuery::create()->findOneById($data['id']);
122
            if ($link) {
123
                $model = $link;
124
            }
125
        }
126
127
        if (isset($data["brand_id"]) && isset($data["dealer_id"])) {
128
            $link = DealerBrandQuery::create()->filterByDealerId($data["dealer_id"])->filterByBrandId($data["brand_id"])->findOne();
129
            if ($link) {
130
                throw new \Exception("A link already exist", 403);
131
            }
132
133
            $model->setBrandId($data["brand_id"]);
134
            $model->setDealerId($data["dealer_id"]);
135
        }
136
137
        return $model;
138
    }
139
}
140