DbalPasteRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 11
dl 0
loc 91
ccs 0
cts 52
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getById() 0 19 2
A save() 0 38 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nastoletni\Code\Infrastructure\Dbal;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\ConnectionException;
9
use Doctrine\DBAL\DBALException;
10
use Nastoletni\Code\Domain\Paste;
11
use Nastoletni\Code\Domain\PasteRepository;
12
use PDO;
13
14
class DbalPasteRepository implements PasteRepository
15
{
16
    /**
17
     * @var Connection
18
     */
19
    private $dbal;
20
21
    /**
22
     * @var DbalPasteMapper
23
     */
24
    private $mapper;
25
26
    /**
27
     * DbalPasteRepository constructor.
28
     *
29
     * @param Connection      $dbal
30
     * @param DbalPasteMapper $mapper
31
     */
32
    public function __construct(Connection $dbal, DbalPasteMapper $mapper)
33
    {
34
        $this->dbal = $dbal;
35
        $this->mapper = $mapper;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getById(Paste\Id $id): Paste
42
    {
43
        $qb = $this->dbal->createQueryBuilder()
44
            ->select('p.id', 'p.title', 'p.created_at', 'f.filename', 'f.content')
45
            ->from('pastes', 'p')
46
            ->innerJoin('p', 'files', 'f', 'f.paste_id = p.id')
47
            ->where('p.id = :id')
48
            ->setParameter(':id', $id->getBase10Id());
49
50
        $query = $this->dbal->executeQuery($qb->getSQL(), $qb->getParameters());
51
52
        if ($query->rowCount() < 1) {
53
            throw new Paste\NotExistsException(sprintf('Paste with id %s does not exist.', $id->getBase62Id()));
54
        }
55
56
        $data = $query->fetchAll();
57
58
        return $this->mapper->map($data);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws ConnectionException when there is something wrong with transaction.
65
     */
66
    public function save(Paste $paste): void
67
    {
68
        $this->dbal->beginTransaction();
69
70
        try {
71
            $this->dbal->insert('pastes', [
72
                'id'         => $paste->getId()->getBase10Id(),
73
                'title'      => $paste->getTitle(),
74
                'created_at' => $paste->getCreatedAt()->format('Y-m-d H:i:s'),
75
            ], [PDO::PARAM_INT, PDO::PARAM_STR, PDO::PARAM_STR]);
76
        } catch (DBALException $e) {
77
            /* @see https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html#error_er_dup_entry */
78
            if (1062 == $e->getCode()) {
79
                throw new Paste\AlreadyExistsException(sprintf(
80
                    'Paste with id %s already exists.',
81
                    $paste->getId()->getBase62Id()
82
                ));
83
            }
84
85
            throw $e;
86
        }
87
88
        // Insertion of files.
89
        foreach ($paste->getFiles() as $file) {
90
            $this->dbal->insert('files', [
91
                'paste_id' => $paste->getId()->getBase10Id(),
92
                'filename' => $file->getFilename(),
93
                'content'  => $file->getContent(),
94
            ], [PDO::PARAM_INT, PDO::PARAM_STR, PDO::PARAM_STR]);
95
        }
96
97
        try {
98
            $this->dbal->commit();
99
        } catch (ConnectionException $e) {
100
            $this->dbal->rollBack();
101
            throw $e;
102
        }
103
    }
104
}
105