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

DoctrineCompanyRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
c 0
b 0
f 0
dl 0
loc 138
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A existsWithName() 0 9 1
A __construct() 0 3 1
A add() 0 11 1
A findOneByUuid() 0 33 1
A getData() 0 15 1
A delete() 0 5 1
A update() 0 12 1
A companyExist() 0 8 1
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
61
(:uuid, :name, :address, :zip_code, :town, :country, :phone, :facsimile, :email, :contact_name, :cellphone, :slug)'
62
        );
63
        $statement->execute($data);
64
    }
65
66
    /**
67
     * @throws Exception
68
     * @throws \Doctrine\DBAL\Driver\Exception
69
     */
70
    final public function update(Company $company): void
71
    {
72
        $data = $this->getData($company);
73
74
        $statement = $this->connection->prepare(
75
            'UPDATE company SET
76
name = :name, address = :address, zip_code = :zip_code, town = :town, country = :country, phone = :phone,
77
facsimile = :facsimile, email = :email, contact_name = :contact_name, cellphone = :cellphone, slug = :slug
78
WHERE uuid = :uuid
79
'
80
        );
81
        $statement->execute($data);
82
    }
83
84
    /**
85
     * @throws Exception
86
     * @throws \Doctrine\DBAL\Driver\Exception
87
     */
88
    final public function delete(string $uuid): void
89
    {
90
        $statement = $this->connection->prepare('DELETE FROM company WHERE uuid = :uuid');
91
        $statement->bindParam('uuid', $uuid);
92
        $statement->execute();
93
    }
94
95
    /**
96
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
97
     */
98
    final public function findOneByUuid(string $uuid): ?Company
99
    {
100
        $query = <<<'SQL'
101
SELECT
102
    company.uuid as uuid,
103
    company.name as name,
104
    company.address as address,
105
    company.zip_code as zipCode,
106
    company.town as town,
107
    company.country as country,
108
    company.phone as phone,
109
    company.facsimile as facsimile,
110
    company.email as email,
111
    company.contact_name as contactName,
112
    company.cellphone as cellphone,
113
    company.slug as slug
114
FROM company
115
WHERE uuid = :uuid
116
SQL;
117
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
118
119
        return Company::create(
120
            ContactUuid::fromString($result['uuid']),
121
            NameField::fromString($result['name']),
122
            $result['address'],
123
            $result['zipCode'],
124
            $result['town'],
125
            $result['country'],
126
            PhoneField::fromString($result['phone']),
127
            PhoneField::fromString($result['facsimile']),
128
            EmailField::fromString($result['email']),
129
            $result['contactName'],
130
            PhoneField::fromString($result['cellphone'])
131
        );
132
    }
133
134
    /**
135
     * @throws Exception
136
     * @throws \Doctrine\DBAL\Driver\Exception
137
     */
138
    final public function companyExist(): bool
139
    {
140
        $query = <<<'SQL'
141
SELECT uuid FROM company
142
SQL;
143
        $statement = $this->connection->executeQuery($query)->fetchOne();
144
145
        return !(false === $statement);
146
    }
147
148
    private function getData(Company $company): array
149
    {
150
        return [
151
            'uuid' => $company->uuid(),
152
            'name' => $company->name(),
153
            'address' => $company->address(),
154
            'zip_code' => $company->zipCode(),
155
            'town' => $company->town(),
156
            'country' => $company->country(),
157
            'phone' => $company->phone(),
158
            'facsimile' => $company->facsimile(),
159
            'email' => $company->email(),
160
            'contact_name' => $company->contact(),
161
            'cellphone' => $company->cellphone(),
162
            'slug' => $company->slug(),
163
        ];
164
    }
165
}
166