Passed
Push — feature-refactor-orm-less ( 89b81a )
by Laurent
02:06
created

DoctrineSupplierRepository::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 19
rs 9.7
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, delay_delivery, order_days, slug, active)
60
VALUES (:uuid, :name, :address, :zip_code, :town, :country, :phone, :facsimile, :email, :contact_name, :cellphone, :family_log, :delay_delivery, :order_days, :slug, :active)'
61
        );
62
        $statement->execute($data);
63
    }
64
65
    /**
66
     * @throws \Doctrine\DBAL\Exception|Exception
67
     */
68
    public function update(Supplier $supplier): void
69
    {
70
        $data = $this->getData($supplier);
71
72
        $statement = $this->connection->prepare(
73
            'UPDATE supplier SET
74
uuid = :uuid, name = :name, address = :address, zip_code = :zip_code, town = :town, country = :country, phone = :phone,
75
facsimile = :facsimile, email = :email, contact_name = :contact_name, cellphone = :cellphone, family_log = :family_log,
76
delay_delivery = :delay_delivery, order_days = :order_days, slug = :slug, active = :active
77
WHERE uuid = :uuid'
78
        );
79
        $statement->execute($data);
80
    }
81
82
    /**
83
     * @throws \Doctrine\DBAL\Exception|Exception
84
     */
85
    public function findOneByUuid(string $uuid): Supplier
86
    {
87
        $query = <<<'SQL'
88
SELECT
89
    supplier.uuid as uuid,
90
    supplier.name as name,
91
    supplier.address as address,
92
    supplier.zip_code as zipCode,
93
    supplier.town as town,
94
    supplier.country as country,
95
    supplier.phone as phone,
96
    supplier.facsimile as facsimile,
97
    supplier.email as email,
98
    supplier.contact_name as contact,
99
    supplier.cellphone as cellphone,
100
    supplier.family_log as familyLog,
101
    supplier.delay_delivery as delayDelivery,
102
    supplier.order_days as orderDays,
103
    supplier.active as active,
104
    supplier.slug as slug
105
FROM supplier
106
WHERE uuid = :uuid
107
SQL;
108
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
109
110
        if (null === $result) {
111
            throw new SupplierNotFound();
112
        }
113
114
        return Supplier::create(
115
            ContactUuid::fromString($result['uuid']),
116
            NameField::fromString($result['name']),
117
            $result['address'],
118
            $result['zipCode'],
119
            $result['town'],
120
            $result['country'],
121
            PhoneField::fromString($result['phone']),
122
            PhoneField::fromString($result['facsimile']),
123
            EmailField::fromString($result['email']),
124
            $result['contact'],
125
            PhoneField::fromString($result['cellphone']),
126
            FamilyLog::create(NameField::fromString($result['familyLog'])),
127
            (int) $result['delayDelivery'],
128
            \explode(',', $result['orderDays']),
129
        );
130
    }
131
132
    private function getData(Supplier $supplier): array
133
    {
134
        return [
135
            'uuid' => $supplier->uuid(),
136
            'name' => $supplier->name(),
137
            'address' => $supplier->address(),
138
            'zip_code' => $supplier->zipCode(),
139
            'town' => $supplier->town(),
140
            'country' => $supplier->country(),
141
            'phone' => $supplier->phone(),
142
            'facsimile' => $supplier->facsimile(),
143
            'email' => $supplier->email(),
144
            'contact_name' => $supplier->contact(),
145
            'cellphone' => $supplier->cellphone(),
146
            'family_log' => $supplier->familyLog(),
147
            'delay_delivery' => $supplier->delayDelivery(),
148
            'order_days' => \implode(',', $supplier->orderDays()),
149
            'slug' => $supplier->slug(),
150
            'active' => (int) $supplier->isActive(),
151
        ];
152
    }
153
}
154