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

DoctrineSupplierRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 10
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\Persistence\DoctrineOrm\Repositories;
15
16
use Administration\Domain\Protocol\Repository\SupplierRepositoryProtocol;
17
use Administration\Domain\Supplier\Model\Supplier;
18
use Administration\Infrastructure\Finders\Exceptions\SupplierNotFound;
19
use Core\Domain\Common\Model\Dependent\FamilyLog;
20
use Core\Domain\Common\Model\VO\ContactUuid;
21
use Core\Domain\Common\Model\VO\EmailField;
22
use Core\Domain\Common\Model\VO\NameField;
23
use Core\Domain\Common\Model\VO\PhoneField;
24
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...
25
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...
26
27
class DoctrineSupplierRepository implements SupplierRepositoryProtocol
28
{
29
    protected Connection $connection;
30
31
    public function __construct(Connection $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * @throws \Doctrine\DBAL\Exception|Exception
38
     */
39
    public function existsWithName(string $name): bool
40
    {
41
        $query = <<<'SQL'
42
SELECT uuid FROM supplier
43
WHERE name = :name
44
SQL;
45
        $statement = $this->connection->executeQuery($query, ['name' => $name])->fetchOne();
46
47
        return !(false === $statement);
48
    }
49
50
    /**
51
     * @throws \Doctrine\DBAL\Exception|Exception
52
     */
53
    public function add(Supplier $supplier): void
54
    {
55
        $data = $this->getData($supplier);
56
57
        $statement = $this->connection->prepare(
58
            'INSERT INTO supplier
59
(uuid, name, address, zip_code, town, country, phone, facsimile, email, contact_name, cellphone, family_log,
60
 delay_delivery, order_days, slug, active) VALUES (:uuid, :name, :address, :zip_code, :town, :country, :phone,
61
:facsimile, :email, :contact_name, :cellphone, :family_log, :delay_delivery, :order_days, :slug, :active)'
62
        );
63
        $statement->execute($data);
64
    }
65
66
    /**
67
     * @throws \Doctrine\DBAL\Exception|Exception
68
     */
69
    public function update(Supplier $supplier): void
70
    {
71
        $data = $this->getData($supplier);
72
73
        $statement = $this->connection->prepare(
74
            'UPDATE supplier SET
75
uuid = :uuid, name = :name, address = :address, zip_code = :zip_code, town = :town, country = :country, phone = :phone,
76
facsimile = :facsimile, email = :email, contact_name = :contact_name, cellphone = :cellphone, family_log = :family_log,
77
delay_delivery = :delay_delivery, order_days = :order_days, slug = :slug, active = :active
78
WHERE uuid = :uuid'
79
        );
80
        $statement->execute($data);
81
    }
82
83
    /**
84
     * @throws \Doctrine\DBAL\Exception|Exception
85
     */
86
    public function findOneByUuid(string $uuid): Supplier
87
    {
88
        $query = <<<'SQL'
89
SELECT
90
    supplier.uuid as uuid,
91
    supplier.name as name,
92
    supplier.address as address,
93
    supplier.zip_code as zipCode,
94
    supplier.town as town,
95
    supplier.country as country,
96
    supplier.phone as phone,
97
    supplier.facsimile as facsimile,
98
    supplier.email as email,
99
    supplier.contact_name as contact,
100
    supplier.cellphone as cellphone,
101
    supplier.family_log as familyLog,
102
    supplier.delay_delivery as delayDelivery,
103
    supplier.order_days as orderDays,
104
    supplier.active as active,
105
    supplier.slug as slug
106
FROM supplier
107
WHERE uuid = :uuid
108
SQL;
109
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
110
111
        if (null === $result) {
112
            throw new SupplierNotFound();
113
        }
114
115
        return Supplier::create(
116
            ContactUuid::fromString($result['uuid']),
117
            NameField::fromString($result['name']),
118
            $result['address'],
119
            $result['zipCode'],
120
            $result['town'],
121
            $result['country'],
122
            PhoneField::fromString($result['phone']),
123
            PhoneField::fromString($result['facsimile']),
124
            EmailField::fromString($result['email']),
125
            $result['contact'],
126
            PhoneField::fromString($result['cellphone']),
127
            FamilyLog::create(NameField::fromString($result['familyLog'])),
128
            (int) $result['delayDelivery'],
129
            \explode(',', $result['orderDays']),
130
        );
131
    }
132
133
    private function getData(Supplier $supplier): array
134
    {
135
        return [
136
            'uuid' => $supplier->uuid(),
137
            'name' => $supplier->name(),
138
            'address' => $supplier->address(),
139
            'zip_code' => $supplier->zipCode(),
140
            'town' => $supplier->town(),
141
            'country' => $supplier->country(),
142
            'phone' => $supplier->phone(),
143
            'facsimile' => $supplier->facsimile(),
144
            'email' => $supplier->email(),
145
            'contact_name' => $supplier->contact(),
146
            'cellphone' => $supplier->cellphone(),
147
            'family_log' => $supplier->familyLog(),
148
            'delay_delivery' => $supplier->delayDelivery(),
149
            'order_days' => \implode(',', $supplier->orderDays()),
150
            'slug' => $supplier->slug(),
151
            'active' => (int) $supplier->isActive(),
152
        ];
153
    }
154
}
155