Completed
Push — master ( 953bfd...5be2ea )
by Antony
02:34
created

ApiController::getContacts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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\Map\TableMap;
15
use Symfony\Component\Config\Definition\Exception\Exception;
16
use Thelia\Controller\Api\BaseApiController;
17
use Thelia\Core\HttpFoundation\JsonResponse;
18
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
    public function defaultAction()
31
    {
32
        $return = [];
33
        $code = 200;
34
35
        try {
36
            $query = DealerQuery::create()
37
                ->joinWithI18n($this->getLocale())
38
                ->filterByVisible(1);
39
40
            if (null != $id = $this->getRequest()->get("dealer_id")) {
41
                $query->filterById($id);
42
            }
43
44
            $return["total"] = $query->count();
45
46
            $query->limit($this->getLimit());
47
            $return["limit"] = $this->getLimit();
48
49
50
            if ($this->getPageOffset() != 0) {
51
                $query->offset($this->getPageOffset());
52
                $return["offset"] = $this->getPageOffset();
53
            }
54
            if ($this->getOffset() != 0) {
55
                $query->offset($this->getOffset());
56
                $return["offset"] = $this->getOffset();
57
            }
58
59
            $dealers = $query->find();
60
61
            $return["data"] = [];
62
63
            /** @var Dealer $dealer */
64
            foreach ($dealers as $dealer) {
65
                $dataI18n = $dealer->getDealerI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME);
66
                $dataRow = array_merge($dealer->toArray(TableMap::TYPE_FIELDNAME), $dataI18n);
67
                $dataRow["contacts"] = $this->getContacts($dealer);
68
                $dataRow["default_schedules"] = $this->getDefaultSchedules($dealer);
69
                $dataRow["extra_schedules"] = $this->getExtraSchedules($dealer);
70
71
                $return["data"][] = $dataRow;
72
            }
73
74
        } 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...
75
            $code = 500;
76
            $return["error"] = $e->getMessage();
77
        }
78
79
80
        return new JsonResponse($return, $code);
81
    }
82
83
    protected function getContacts(Dealer $dealer)
84
    {
85
86
        $return = [];
87
        foreach ($dealer->getDealerContacts() as $dealerContact) {
88
            $dataRow = $dealerContact->toArray(TableMap::TYPE_FIELDNAME);
89
            $dataRow = array_merge($dataRow, $dealerContact->getDealerContactI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME));
90
            $dataRow["data"] = $this->getContactInfo($dealerContact);
91
            $return[] = $dataRow;
92
        }
93
        return $return;
94
    }
95
96
    protected function getContactInfo(DealerContact $contact)
97
    {
98
        $return = [];
99
        foreach ($contact->getDealerContactInfos() as $dealerContact) {
100
            $dataRow = $dealerContact->toArray(TableMap::TYPE_FIELDNAME);
101
            $dataRow = array_merge($dataRow, $dealerContact->getDealerContactInfoI18ns()->getData()[0]->toArray(TableMap::TYPE_FIELDNAME));
102
            $return[] = $dataRow;
103
        }
104
        return $return;
105
    }
106
107
    protected function getDefaultSchedules(Dealer $dealer)
108
    {
109
        $return = [];
110
        foreach ($dealer->getDefaultSchedules() as $schedules) {
111
            $return[] = $schedules->toArray(TableMap::TYPE_FIELDNAME);
112
        }
113
        return $return;
114
    }
115
116
    protected function getExtraSchedules(Dealer $dealer)
117
    {
118
        $return = [];
119
        foreach ($dealer->getExtraSchedules() as $schedules) {
120
            $return[] = $schedules->toArray(TableMap::TYPE_FIELDNAME);
121
        }
122
        return $return;
123
    }
124
125
    protected function getLimit()
126
    {
127
        $limit = $this->getRequest()->get("limit");
128
        return ($limit) ? $limit : static::DEFAULT_LIMIT;
129
    }
130
131
    protected function getOffset()
132
    {
133
        $offset = $this->getRequest()->get("offset");
134
        return ($offset) ? $offset : 0;
135
    }
136
137
    protected function getPageOffset()
138
    {
139
        $page = $this->getRequest()->get("page");
140
        return ($page) ? ($page - 1) * $this->getLimit() : 0;
141
    }
142
143
    protected function getLocale()
144
    {
145
        $locale = $this->getRequest()->get("locale");
146
        return ($locale) ? $locale : 'fr_FR';
147
    }
148
}