Passed
Push — develop ( 6aee1f...eb8792 )
by Laurent
01:33
created

DoctrineSupplierFinder::findAllActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 42
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 46
rs 9.248
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Infrastructure\Finders\Doctrine;
15
16
use Administration\Application\Protocol\Finders\SupplierFinderProtocol;
17
use Administration\Application\Supplier\ReadModel\Supplier as SupplierModel;
18
use Administration\Application\Supplier\ReadModel\Suppliers;
19
use Administration\Infrastructure\Finders\Exceptions\SupplierNotFound;
20
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection 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...
21
use Doctrine\DBAL\Driver\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Driver\Exception 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...
22
23
class DoctrineSupplierFinder implements SupplierFinderProtocol
24
{
25
    private Connection $connection;
26
27
    public function __construct(Connection $connection)
28
    {
29
        $this->connection = $connection;
30
    }
31
32
    /**
33
     * @throws \Doctrine\DBAL\Exception|Exception
34
     */
35
    public function findOneByUuid(string $uuid): SupplierModel
36
    {
37
        $query = <<<'SQL'
38
SELECT
39
    supplier.uuid as uuid,
40
    supplier.name as name,
41
    supplier.address as address,
42
    supplier.zip_code as zipCode,
43
    supplier.town as town,
44
    supplier.country as country,
45
    supplier.phone as phone,
46
    supplier.facsimile as facsimile,
47
    supplier.email as email,
48
    supplier.contact_name as contact,
49
    supplier.cellphone as cellphone,
50
    supplier.family_log as familyLog,
51
    supplier.delay_delivery as delayDelivery,
52
    supplier.order_days as orderDays,
53
    supplier.active as active,
54
    supplier.slug as slug
55
FROM supplier
56
WHERE uuid = :uuid
57
SQL;
58
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
59
60
        if (null === $result) {
61
            throw new SupplierNotFound();
62
        }
63
64
        return new SupplierModel(
65
            $result['uuid'],
66
            $result['name'],
67
            $result['address'],
68
            $result['zipCode'],
69
            $result['town'],
70
            $result['country'],
71
            $result['phone'],
72
            $result['facsimile'],
73
            $result['email'],
74
            $result['contact'],
75
            $result['cellphone'],
76
            $result['familyLog'],
77
            (int) $result['delayDelivery'],
78
            \explode(',', $result['orderDays']),
79
            $result['slug'],
80
            $result['active']
81
        );
82
    }
83
84
    /**
85
     * @throws \Doctrine\DBAL\Exception|Exception
86
     */
87
    public function findAllActive(): Suppliers
88
    {
89
        $query = <<<'SQL'
90
SELECT
91
    supplier.uuid as uuid,
92
    supplier.name as name,
93
    supplier.address as address,
94
    supplier.zip_code as zipCode,
95
    supplier.town as town,
96
    supplier.country as country,
97
    supplier.phone as phone,
98
    supplier.facsimile as facsimile,
99
    supplier.email as email,
100
    supplier.contact_name as contact,
101
    supplier.cellphone as cellphone,
102
    supplier.family_log as familyLog,
103
    supplier.delay_delivery as delayDelivery,
104
    supplier.order_days as orderDays,
105
    supplier.active as active,
106
    supplier.slug as slug
107
FROM supplier
108
WHERE active = 1
109
SQL;
110
        $result = $this->connection->executeQuery($query)->fetchAllAssociative();
111
112
        return new Suppliers(
113
            ...\array_map(static function (array $supplier) {
114
                return new SupplierModel(
115
                    $supplier['uuid'],
116
                    $supplier['name'],
117
                    $supplier['address'],
118
                    $supplier['zipCode'],
119
                    $supplier['town'],
120
                    $supplier['country'],
121
                    $supplier['phone'],
122
                    $supplier['facsimile'],
123
                    $supplier['email'],
124
                    $supplier['contact'],
125
                    $supplier['cellphone'],
126
                    $supplier['familyLog'],
127
                    (int) $supplier['delayDelivery'],
128
                    \explode(',', $supplier['orderDays']),
129
                    $supplier['slug'],
130
                    (bool) $supplier['active']
131
                );
132
            }, $result)
133
        );
134
    }
135
}
136