ClientRepository::updateData()   C
last analyzed

Complexity

Conditions 13
Paths 158

Size

Total Lines 76
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 39
c 1
b 0
f 0
nc 158
nop 2
dl 0
loc 76
rs 6.1333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ControleOnline\Repository;
4
5
use ControleOnline\Entity\Client;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use ControleOnline\Entity\Status;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Status was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use ControleOnline\Entity\People;
8
use ControleOnline\Entity\PeopleLinkClient;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\PeopleLinkClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ControleOnline\Entity\PeopleSalesman;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\PeopleSalesman was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use ControleOnline\Entity\Order;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Doctrine\ORM\Query\ResultSetMapping;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Query\ResultSetMapping was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * @method Client|null find($id, $lockMode = null, $lockVersion = null)
17
 * @method Client|null findOneBy(array $criteria, array $orderBy = null)
18
 * @method Client[]    findAll()
19
 * @method Client[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
20
 */
21
class ClientRepository extends ServiceEntityRepository
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, Client::class);
26
    }
27
28
    public function updateData(int $clientId, array $data): void
29
    {
30
        $rawSQL = '';
31
        $params = [];
32
33
        try {
34
35
            $this->getEntityManager()->getConnection()->executeQuery('START TRANSACTION', $params);
36
37
            // update client document
38
39
            if (isset($data['document']) && !empty($data['document'])) {
40
                $rawSQL = 'UPDATE document SET document = :document WHERE people_id = :people_id AND document_type_id = 3';
41
                $params = [
42
                    'people_id' => $clientId,
43
                    'document'  => $data['document'],
44
                ];
45
46
                $this->getEntityManager()->getConnection()->executeQuery($rawSQL, $params);
47
            }
48
49
            // update client name
50
51
            if (isset($data['name']) && !empty($data['name'])) {
52
                $rawSQL = 'UPDATE people SET name = :name WHERE id = :people_id';
53
                $params = [
54
                    'people_id' => $clientId,
55
                    'name'      => $data['name'],
56
                ];
57
58
                $this->getEntityManager()->getConnection()->executeQuery($rawSQL, $params);
59
            }
60
61
            // update client alias
62
63
            if (isset($data['alias']) && !empty($data['alias'])) {
64
                $rawSQL = 'UPDATE people SET alias = :alias WHERE id = :people_id';
65
                $params = [
66
                    'people_id' => $clientId,
67
                    'alias'     => $data['alias'],
68
                ];
69
70
                $this->getEntityManager()->getConnection()->executeQuery($rawSQL, $params);
71
            }
72
73
            // update client email
74
75
            if (isset($data['email']) && !empty($data['email'])) {
76
                $rawSQL = 'UPDATE email SET email = :email WHERE people_id = :people_id';
77
                $params = [
78
                    'people_id' => $clientId,
79
                    'email'     => $data['email'],
80
                ];
81
82
                $this->getEntityManager()->getConnection()->executeQuery($rawSQL, $params);
83
            }
84
85
            // update client phone
86
87
            if (isset($data['phone']) && is_array($data['phone']) && !empty($data['phone'])) {
88
                $rawSQL = 'UPDATE phone SET ddd = :ddd, phone = :phone WHERE people_id = :people_id';
89
                $params = [
90
                    'people_id' => $clientId,
91
                    'ddd'       => $data['phone']['ddd'],
92
                    'phone'     => $data['phone']['phone'],
93
                ];
94
95
                $this->getEntityManager()->getConnection()->executeQuery($rawSQL, $params);
96
            }
97
98
            $this->getEntityManager()->getConnection()->executeQuery('COMMIT', $params);
99
100
        } catch (\Exception $e) {
101
            $this->getEntityManager()->getConnection()->executeQuery('ROLLBACK', $params);
102
103
            throw new \Exception('Error updating client data');
104
        }
105
    }
106
107
    public function getSalesmanClientCollection(string $type, string $fromDate, string $toDate, People $provider, People $salesman)
108
    {
109
        $queryBuilder = $this->createQueryBuilder('myclients');
110
111
        switch ($type) {
112
113
            case 'active':
114
115
                $queryBuilder->innerJoin(Order::class,     'O'  , 'WITH', 'O.client = myclients.id');
116
                $queryBuilder->innerJoin(People::class,         'C'  , 'WITH', 'C.id = O.provider');
117
                $queryBuilder->innerJoin(PeopleSalesman::class, 'kkk', 'WITH', 'kkk.company = C.id');
118
                $queryBuilder->innerJoin(People::class,         'S'  , 'WITH', 'S.id = kkk.salesman');
119
                $queryBuilder->innerJoin(PeopleClient::class,   'PC' , 'WITH', 'PC.client = myclients.id AND PC.company_id = S.id');
0 ignored issues
show
Bug introduced by
The type ControleOnline\Repository\PeopleClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
                $queryBuilder->andWhere('O.provider = :provider');
121
                $queryBuilder->andWhere('S.id IN (:my_companies)');
122
                $queryBuilder->andWhere('O.status NOT IN (:statuses)');
123
                $queryBuilder->andWhere('O.orderDate BETWEEN :from_date AND :to_date');
124
                $queryBuilder->setParameter('provider'    , $provider);
125
                $queryBuilder->setParameter('statuses'    , $this->getEntityManager()->getRepository(Status::class)->findBy(['realStatus' => ['open','canceled']]));
126
                $queryBuilder->setParameter('from_date'   , $fromDate);
127
                $queryBuilder->setParameter('to_date'     , $toDate);
128
                $queryBuilder->setParameter('my_companies', $this->getMyCompanies($salesman));
129
130
            break;
131
132
            case 'inactive':
133
134
                $queryBuilder->innerJoin(Order::class,     'O'  , 'WITH', 'O.client = myclients.id');
135
                $queryBuilder->innerJoin(People::class,         'C'  , 'WITH', 'C.id = O.provider');
136
                $queryBuilder->innerJoin(PeopleSalesman::class, 'kkk', 'WITH', 'kkk.company = C.id');
137
                $queryBuilder->innerJoin(People::class,         'S'  , 'WITH', 'S.id = kkk.salesman');
138
                $queryBuilder->innerJoin(PeopleClient::class,   'PC' , 'WITH', 'PC.client = myclients.id AND PC.company_id = S.id');
139
                $queryBuilder->leftJoin (Order::class,     'jj' , 'WITH', 'jj.client = myclients.id
140
                                                                             AND   (jj.status NOT IN (:statuses) OR jj.id IS NULL)
141
                                                                             AND   ((jj.orderDate  BETWEEN  :from_date AND :to_date) OR jj.id IS NULL)
142
                                                                             AND   (jj.provider = :provider  OR jj.id IS NULL)');
143
                $queryBuilder->andWhere('O.provider = :provider');
144
                $queryBuilder->andWhere('S.id IN (:my_companies)');
145
                $queryBuilder->andWhere('O.status NOT IN (:statuses)');
146
                $queryBuilder->andWhere('O.orderDate  NOT BETWEEN :from_date AND :to_date');
147
                $queryBuilder->groupBy('myclients.id');
148
                $queryBuilder->having('COUNT(jj.id) > :count');
149
                $queryBuilder->andHaving('COUNT(O.id) = :count');
150
                $queryBuilder->setParameter('provider' , $provider);
151
                $queryBuilder->setParameter('statuses' , $this->getEntityManager()->getRepository(Status::class)->findBy(['realStatus' => ['open','canceled']]));
152
                $queryBuilder->setParameter('from_date', $fromDate);
153
                $queryBuilder->setParameter('to_date'  , $toDate);
154
                $queryBuilder->setParameter('count'    , 0);
155
                $queryBuilder->setParameter('my_companies', $this->getMyCompanies($salesman));
156
157
            break;
158
159
            case 'prospect':
160
161
                $queryBuilder->innerJoin(Order::class,     'O' , 'WITH', 'O.client = myclients.id');
162
                $queryBuilder->innerJoin(People::class,         'C' , 'WITH', 'C.id = O.provider');
163
                $queryBuilder->innerJoin(PeopleSalesman::class, 'PS', 'WITH', 'PS.company = C.id');
164
                $queryBuilder->innerJoin(People::class,         'S' , 'WITH', 'S.id = PS.salesman');
165
                $queryBuilder->innerJoin(PeopleClient::class,   'PC', 'WITH', 'PC.client = myclients.id AND PC.company_id = S.id');
166
                $queryBuilder->leftJoin (Order::class,     'jj' , 'WITH', 'jj.client = myclients.id
167
                                                                             AND   (jj.status NOT IN (:statuses) OR jj.id IS NULL)                                                                             
168
                                                                             AND   (jj.provider = :provider  OR jj.id IS NULL)');
169
                $queryBuilder->andWhere('O.provider = :provider');
170
                $queryBuilder->andWhere('S.id IN (:my_companies)');
171
                $queryBuilder->andWhere('O.status NOT IN (:statuses)');
172
                $queryBuilder->groupBy('myclients.id');
173
                $queryBuilder->having('COUNT(jj.id) = :count');
174
175
                $queryBuilder->setParameter('provider', $provider);
176
                $queryBuilder->setParameter('statuses', $this->getEntityManager()->getRepository(Status::class)->findBy(['realStatus' => ['open','canceled']]));
177
                $queryBuilder->setParameter('count'   , 0);
178
                $queryBuilder->setParameter('my_companies', $this->getMyCompanies($salesman));
179
180
            break;
181
182
            case 'new':
183
184
                $queryBuilder->innerJoin(Order::class,     'O'  , 'WITH', 'O.client = myclients.id');
185
                $queryBuilder->innerJoin(People::class,         'C'  , 'WITH', 'C.id = O.provider');
186
                $queryBuilder->innerJoin(PeopleSalesman::class, 'kkk', 'WITH', 'kkk.company = C.id');
187
                $queryBuilder->innerJoin(People::class,         'S'  , 'WITH', 'S.id = kkk.salesman');
188
                $queryBuilder->innerJoin(PeopleClient::class,   'PC' , 'WITH', 'PC.client = myclients.id AND PC.company_id = S.id');
189
190
                $queryBuilder->andWhere('O.provider = :provider');
191
                $queryBuilder->andWhere('S.id IN (:my_companies)');
192
                $queryBuilder->andWhere('O.status NOT IN (:statuses)');
193
                $queryBuilder->andWhere('O.orderDate    BETWEEN :from_date AND :to_date');
194
                $queryBuilder->andWhere('myclients.registerDate BETWEEN :from_date AND :to_date');
195
196
                $queryBuilder->setParameter('provider'    , $provider);
197
                $queryBuilder->setParameter('statuses'    , $this->getEntityManager()->getRepository(Status::class)->findBy(['realStatus' => ['open','canceled']]));
198
                $queryBuilder->setParameter('from_date'   , $fromDate);
199
                $queryBuilder->setParameter('to_date'     , $toDate);
200
                $queryBuilder->setParameter('my_companies', $this->getMyCompanies($salesman));
201
202
            break;
203
204
            default:
205
                $queryBuilder->andWhere('myclients.id = 0');
206
            break;
207
        }
208
209
        return $queryBuilder->getQuery()->getResult();
210
    }
211
212
    private function getMyCompanies(People $salesman): array
213
    {
214
        $sql = "
215
            SELECT
216
              pee.company_id
217
            FROM people_employee pee
218
            WHERE
219
              pee.employee_id = :employee_id
220
        ";
221
222
        $rsm = new ResultSetMapping();
223
224
        $rsm->addScalarResult('company_id', 'company_id', 'integer');
225
226
        $nqu = $this->getEntityManager()->createNativeQuery($sql, $rsm);
227
228
        $nqu->setParameter('employee_id', $salesman->getId());
229
230
        return $nqu->getArrayResult();
231
    }
232
}
233