Completed
Push — master ( 5be2ea...28be85 )
by Antony
02:38
created

ApiController::afterProcessDealer()   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
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: apenalver
5
 * Date: 14/03/2016
6
 * Time: 10:22
7
 */
8
9
namespace Dealer\Controller;
10
11
use Dealer\Model\Dealer;
12
use Dealer\Model\DealerContact;
13
use Dealer\Model\DealerQuery;
14
use Propel\Runtime\ActiveQuery\Criteria;
15
use Propel\Runtime\Map\TableMap;
16
use Symfony\Component\Config\Definition\Exception\Exception;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Thelia\Controller\Api\BaseApiController;
19
20
21
/**
22
 * Class ApiController
23
 * @package Dealer\Controller
24
 */
25
class ApiController extends BaseApiController
26
{
27
28
    const DEFAULT_LIMIT = 10;
29
30
    /**
31
     * @return JsonResponse
32
     */
33
    public function defaultAction()
34
    {
35
        $return = [];
36
        $code = 200;
37
38
        try {
39
            $query = DealerQuery::create()
40
                ->joinWithI18n($this->getLocale())
41
                ->filterByVisible(1);
42
43
            if (null != $id = $this->getRequest()->get("dealer_id")) {
44
                $query->filterById($id);
45
            }
46
47
            $return["total"] = $query->count();
48
49
            $query->limit($this->getLimit());
50
            $return["limit"] = $this->getLimit();
51
52
53
            if ($this->getPageOffset() != 0) {
54
                $query->offset($this->getPageOffset());
55
                $return["offset"] = $this->getPageOffset();
56
            }
57
            if ($this->getOffset() != 0) {
58
                $query->offset($this->getOffset());
59
                $return["offset"] = $this->getOffset();
60
            }
61
62
            $query = $this->addOrder($query);
63
64
            $dealers = $query->find();
65
66
            $return["data"] = [];
67
68
            /** @var Dealer $dealer */
69
            foreach ($dealers as $dealer) {
70
71
                $dataI18n = $dealer->getDealerI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME);
72
                $dataRow = array_merge($dealer->toArray(TableMap::TYPE_FIELDNAME), $dataI18n);
73
                $dataRow["contacts"] = $this->getContacts($dealer);
74
                $dataRow["default_schedules"] = $this->getDefaultSchedules($dealer);
75
                $dataRow["extra_schedules"] = $this->getExtraSchedules($dealer);
76
77
                $dataRow = $this->afterProcessDealer($dataRow, $dealer);
78
79
                $return["data"][] = $dataRow;
80
            }
81
82
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Config...ion\Exception\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
83
            $code = 500;
84
            $return["error"] = $e->getMessage();
85
        }
86
87
88
        return new JsonResponse($return, $code);
89
    }
90
91
    /**
92
     * @param Dealer $dealer
93
     * @return array
94
     */
95
    protected function getContacts(Dealer $dealer)
96
    {
97
98
        $return = [];
99
        foreach ($dealer->getDealerContacts() as $dealerContact) {
100
            $dataRow = $dealerContact->toArray(TableMap::TYPE_FIELDNAME);
101
            $dataRow = array_merge($dataRow, $dealerContact->getDealerContactI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME));
102
            $dataRow["data"] = $this->getContactInfo($dealerContact);
103
            $return[] = $dataRow;
104
        }
105
        return $return;
106
    }
107
108
    /**
109
     * @param DealerContact $contact
110
     * @return array
111
     */
112
    protected function getContactInfo(DealerContact $contact)
113
    {
114
        $return = [];
115
        foreach ($contact->getDealerContactInfos() as $dealerContact) {
116
            $dataRow = $dealerContact->toArray(TableMap::TYPE_FIELDNAME);
117
            $dataRow = array_merge($dataRow, $dealerContact->getDealerContactInfoI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME));
118
            $return[] = $dataRow;
119
        }
120
        return $return;
121
    }
122
123
    /**
124
     * @param Dealer $dealer
125
     * @return array
126
     */
127
    protected function getDefaultSchedules(Dealer $dealer)
128
    {
129
        $return = [];
130
        foreach ($dealer->getDefaultSchedules() as $schedules) {
131
            $return[] = $schedules->toArray(TableMap::TYPE_FIELDNAME);
132
        }
133
        return $return;
134
    }
135
136
    /**
137
     * @param Dealer $dealer
138
     * @return array
139
     */
140
    protected function getExtraSchedules(Dealer $dealer)
141
    {
142
        $return = [];
143
        foreach ($dealer->getExtraSchedules() as $schedules) {
144
            $return[] = $schedules->toArray(TableMap::TYPE_FIELDNAME);
145
        }
146
        return $return;
147
    }
148
149
    /**
150
     * @return int|mixed
151
     */
152
    protected function getLimit()
153
    {
154
        $limit = $this->getRequest()->get("limit");
155
        return ($limit) ? $limit : static::DEFAULT_LIMIT;
156
    }
157
158
    /**
159
     * @return int|mixed
160
     */
161
    protected function getOffset()
162
    {
163
        $offset = $this->getRequest()->get("offset");
164
        return ($offset) ? $offset : 0;
165
    }
166
167
    /**
168
     * @return int|mixed
169
     */
170
    protected function getPageOffset()
171
    {
172
        $page = $this->getRequest()->get("page");
173
        return ($page) ? ($page - 1) * $this->getLimit() : 0;
174
    }
175
176
    /**
177
     * @return mixed|string
178
     */
179
    protected function getLocale()
180
    {
181
        $locale = $this->getRequest()->get("locale");
182
        return ($locale) ? $locale : 'fr_FR';
183
    }
184
185
    /**
186
     * @param DealerQuery $query
187
     * @return DealerQuery
188
     */
189
    protected function addOrder(DealerQuery $query)
190
    {
191
192
        $order = $this->getRequest()->get("order");
193
        switch ($order) {
194
            case "id" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
195
                $query->orderById();
196
                break;
197
            case "id-reverse" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
198
                $query->orderById(Criteria::DESC);
199
                break;
200
            case "date" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
201
                $query->orderByCreatedAt();
202
                break;
203
            case "date-reverse" :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
204
                $query->orderByCreatedAt(Criteria::DESC);
205
                break;
206
            default:
207
                $query->orderById();
208
                break;
209
        }
210
        return $query;
211
    }
212
213
    /**
214
     * @param $dataRow
215
     * @param Dealer $dealer
216
     * @return mixed
217
     */
218
    protected function afterProcessDealer($dataRow, Dealer $dealer)
219
    {
220
        return $dataRow;
221
    }
222
}