Passed
Pull Request — develop (#151)
by Laurent
01:35
created

DoctrineCompanyRepository::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.8333
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\Company\Model\Company;
17
use Administration\Domain\Protocol\Repository\CompanyRepositoryProtocol;
18
use Core\Domain\Common\Model\VO\ContactUuid;
19
use Core\Domain\Common\Model\VO\EmailField;
20
use Core\Domain\Common\Model\VO\NameField;
21
use Core\Domain\Common\Model\VO\PhoneField;
22
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...
23
use Doctrine\DBAL\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\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...
24
25
class DoctrineCompanyRepository implements CompanyRepositoryProtocol
26
{
27
    protected Connection $connection;
28
29
    public function __construct(Connection $connection)
30
    {
31
        $this->connection = $connection;
32
    }
33
34
    /**
35
     * @throws Exception
36
     * @throws \Doctrine\DBAL\Driver\Exception
37
     */
38
    final public function existsWithName(string $name): bool
39
    {
40
        $query = <<<'SQL'
41
SELECT uuid FROM company
42
WHERE name = :name
43
SQL;
44
        $statement = $this->connection->executeQuery($query, ['name' => $name])->fetchOne();
45
46
        return !(false === $statement);
47
    }
48
49
    /**
50
     * @throws Exception
51
     * @throws \Doctrine\DBAL\Driver\Exception
52
     */
53
    final public function add(Company $company): void
54
    {
55
        $data = $this->getData($company);
56
57
        $statement = $this->connection->prepare(
58
            'INSERT INTO company
59
(uuid, name, address, zip_code, town, country, phone, facsimile, email, contact_name, cellphone, slug)
60
VALUES (:uuid, :name, :address, :zip_code, :town, :country, :phone, :facsimile, :email, :contact_name, :cellphone, :slug)'
61
        );
62
        $statement->execute($data);
63
    }
64
65
    /**
66
     * @throws Exception
67
     * @throws \Doctrine\DBAL\Driver\Exception
68
     */
69
    final public function update(Company $company): void
70
    {
71
        $data = $this->getData($company);
72
73
        $statement = $this->connection->prepare(
74
            'UPDATE company SET
75
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, slug = :slug
77
WHERE uuid = :uuid
78
'
79
        );
80
        $statement->execute($data);
81
    }
82
83
    /**
84
     * @throws Exception
85
     * @throws \Doctrine\DBAL\Driver\Exception
86
     */
87
    final public function delete(string $uuid): void
88
    {
89
        $statement = $this->connection->prepare('DELETE FROM company WHERE uuid = :uuid');
90
        $statement->bindParam('uuid', $uuid);
91
        $statement->execute();
92
    }
93
94
    /**
95
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
96
     */
97
    final public function findOneByUuid(string $uuid): ?Company
98
    {
99
        $query = <<<'SQL'
100
SELECT
101
    company.uuid as uuid,
102
    company.name as name,
103
    company.address as address,
104
    company.zip_code as zipCode,
105
    company.town as town,
106
    company.country as country,
107
    company.phone as phone,
108
    company.facsimile as facsimile,
109
    company.email as email,
110
    company.contact_name as contactName,
111
    company.cellphone as cellphone,
112
    company.slug as slug
113
FROM company
114
WHERE uuid = :uuid
115
SQL;
116
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
117
118
        return Company::create(
119
            ContactUuid::fromString($result['uuid']),
120
            NameField::fromString($result['name']),
121
            $result['address'],
122
            $result['zipCode'],
123
            $result['town'],
124
            $result['country'],
125
            PhoneField::fromString($result['phone']),
126
            PhoneField::fromString($result['facsimile']),
127
            EmailField::fromString($result['email']),
128
            $result['contactName'],
129
            PhoneField::fromString($result['cellphone'])
130
        );
131
    }
132
133
    /**
134
     * @throws Exception
135
     * @throws \Doctrine\DBAL\Driver\Exception
136
     */
137
    final public function companyExist(): bool
138
    {
139
        $query = <<<'SQL'
140
SELECT uuid FROM company
141
SQL;
142
        $statement = $this->connection->executeQuery($query)->fetchOne();
143
144
        return !(false === $statement);
145
    }
146
147
    private function getData(Company $company): array
148
    {
149
        return [
150
            'uuid' => $company->uuid(),
151
            'name' => $company->name(),
152
            'address' => $company->address(),
153
            'zip_code' => $company->zipCode(),
154
            'town' => $company->town(),
155
            'country' => $company->country(),
156
            'phone' => $company->phone(),
157
            'facsimile' => $company->facsimile(),
158
            'email' => $company->email(),
159
            'contact_name' => $company->contact(),
160
            'cellphone' => $company->cellphone(),
161
            'slug' => $company->slug(),
162
        ];
163
    }
164
}
165