Passed
Pull Request — develop (#152)
by Laurent
01:25
created

DoctrineCompanyRepository::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 9
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\Company\Model\Company;
17
use Administration\Domain\Protocol\Repository\CompanyRepositoryProtocol;
18
use Administration\Infrastructure\Company\Mapper\CompanyModelMapper;
19
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...
20
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...
21
22
class DoctrineCompanyRepository implements CompanyRepositoryProtocol
23
{
24
    protected Connection $connection;
25
26
    public function __construct(Connection $connection)
27
    {
28
        $this->connection = $connection;
29
    }
30
31
    /**
32
     * @throws Exception
33
     */
34
    final public function existsWithName(string $name): bool
35
    {
36
        $query = $this->connection->createQueryBuilder()
37
            ->select('name')
38
            ->from('company')
39
            ->where('name = :name')
40
            ->setParameter('name', $name)
41
        ;
42
        $statement = $query->execute()->fetchOne();
43
44
        return !(false === $statement);
45
    }
46
47
    /**
48
     * @throws Exception
49
     */
50
    final public function add(Company $company): void
51
    {
52
        $data = (new CompanyModelMapper())->getDataFromCompany($company);
53
54
        $query = $this->connection->createQueryBuilder()
55
            ->insert('company')
56
            ->values([
57
                'uuid' => '?', 'name' => '?', 'address' => '?', 'zip_code' => '?', 'town' => '?', 'country' => '?',
58
                'phone' => '?', 'facsimile' => '?', 'email' => '?', 'contact_name' => '?', 'cellphone' => '?',
59
                'slug' => '?',
60
            ])
61
            ->setParameters([
62
                $data['uuid'], $data['name'], $data['address'], $data['zip_code'], $data['town'],
63
                $data['country'], $data['phone'], $data['facsimile'], $data['email'], $data['contact_name'],
64
                $data['cellphone'], $data['slug'],
65
            ])
66
        ;
67
        $query->execute();
68
    }
69
70
    /**
71
     * @throws Exception
72
     * @throws \Doctrine\DBAL\Driver\Exception
73
     */
74
    final public function update(Company $company): void
75
    {
76
        $data = (new CompanyModelMapper())->getDataFromCompany($company);
77
78
        $statement = $this->connection->prepare(
79
            'UPDATE company SET
80
name = :name, address = :address, zip_code = :zip_code, town = :town, country = :country, phone = :phone,
81
facsimile = :facsimile, email = :email, contact_name = :contact_name, cellphone = :cellphone, slug = :slug
82
WHERE uuid = :uuid
83
'
84
        );
85
        $statement->execute($data);
86
    }
87
88
    /**
89
     * @throws Exception
90
     */
91
    final public function delete(string $uuid): void
92
    {
93
        $statement = $this->connection->createQueryBuilder()
94
            ->delete('company')
95
            ->where('uuid = ?')
96
            ->setParameter(0, $uuid)
97
        ;
98
        $statement->execute();
99
    }
100
101
    /**
102
     * @throws Exception
103
     */
104
    final public function findOneByUuid(string $uuid): ?Company
105
    {
106
        $query = $this->connection->createQueryBuilder()
107
            ->select(
108
                'uuid',
109
                'name',
110
                'address',
111
                'zip_code',
112
                'town',
113
                'country',
114
                'phone',
115
                'facsimile',
116
                'email',
117
                'contact_name',
118
                'cellphone',
119
                'slug'
120
            )
121
            ->from('company')
122
            ->where('uuid = ?')
123
            ->setParameter(0, $uuid)
124
        ;
125
126
        $result = $query->execute()->fetchAssociative();
127
128
        return (new CompanyModelMapper())->getDomainModelFromArray($result);
129
    }
130
131
    /**
132
     * @throws Exception
133
     */
134
    final public function exists(): bool
135
    {
136
        $query = $this->connection->createQueryBuilder()
137
            ->select('uuid')
138
            ->from('company')
139
        ;
140
        $statement = $query->execute()->fetchOne();
141
142
        return false !== $statement;
143
    }
144
}
145