Passed
Push — master ( bf42cc...349842 )
by Peter
05:57
created

FormSqlDataMapper::loadEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Orm\DataMappers;
6
7
use AbterPhp\Contact\Domain\Entities\Form as Entity;
8
use Opulence\Orm\DataMappers\SqlDataMapper;
9
use Opulence\QueryBuilders\MySql\QueryBuilder;
10
use Opulence\QueryBuilders\MySql\SelectQuery;
11
12
class FormSqlDataMapper extends SqlDataMapper implements IFormDataMapper
13
{
14
    /**
15
     * @param Entity $entity
16
     */
17
    public function add($entity)
18
    {
19
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Contact\Domain\Entities\Form.
Loading history...
20
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Form entity.');
21
        }
22
23
        $query = (new QueryBuilder())
24
            ->insert(
25
                'contact_forms',
26
                [
27
                    'id'         => [$entity->getId(), \PDO::PARAM_STR],
28
                    'name'       => [$entity->getName(), \PDO::PARAM_STR],
29
                    'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
30
                    'to_name'    => [$entity->getToName(), \PDO::PARAM_STR],
31
                    'to_email'   => [$entity->getToEmail(), \PDO::PARAM_STR],
32
                ]
33
            );
34
35
        $sql = $query->getSql();
36
37
        $statement = $this->writeConnection->prepare($sql);
38
        $statement->bindValues($query->getParameters());
39
        $statement->execute();
40
    }
41
42
    /**
43
     * @param Entity $entity
44
     */
45
    public function delete($entity)
46
    {
47
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Contact\Domain\Entities\Form.
Loading history...
48
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Form entity.');
49
        }
50
51
        $query = (new QueryBuilder())
52
            ->update('contact_forms', 'contact_forms', ['deleted' => [1, \PDO::PARAM_INT]])
53
            ->where('id = ?')
54
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
55
56
        $sql    = $query->getSql();
57
        $params = $query->getParameters();
58
59
        $statement = $this->writeConnection->prepare($sql);
60
        $statement->bindValues($params);
61
        $statement->execute();
62
    }
63
64
    /**
65
     * @return Entity[]
66
     */
67
    public function getAll(): array
68
    {
69
        $query = $this->getBaseQuery();
70
71
        $sql = $query->getSql();
72
73
        return $this->read($sql, [], self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    /**
77
     * @param int      $limitFrom
78
     * @param int      $pageSize
79
     * @param string[] $orders
80
     * @param array    $conditions
81
     * @param array    $params
82
     *
83
     * @return Entity[]
84
     */
85
    public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array
86
    {
87
        $query = $this->getBaseQuery()
88
            ->limit($pageSize)
89
            ->offset($limitFrom);
90
91
        foreach ($orders as $order) {
92
            $query->addOrderBy($order);
93
        }
94
95
        foreach ($conditions as $condition) {
96
            $query->andWhere($condition);
97
        }
98
99
        $replaceCount = 1;
100
101
        $sql = $query->getSql();
102
        $sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount);
103
104
        return $this->read($sql, $params, self::VALUE_TYPE_ARRAY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...self::VALUE_TYPE_ARRAY) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    /**
108
     * @param int|string $id
109
     *
110
     * @return array|null
111
     */
112
    public function getById($id)
113
    {
114
        $query = $this->getBaseQuery()->andWhere('cf.id = :form_id');
115
116
        $parameters = [
117
            'form_id' => [$id, \PDO::PARAM_STR],
118
        ];
119
120
        $sql = $query->getSql();
121
122
        return $this->read($sql, $parameters, self::VALUE_TYPE_ENTITY, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->read($sql,...ALUE_TYPE_ENTITY, true) also could return the type array which is incompatible with the return type mandated by Opulence\Orm\DataMappers\IDataMapper::getById() of object.
Loading history...
123
    }
124
125
    /**
126
     * @param Entity $entity
127
     */
128
    public function update($entity)
129
    {
130
        if (!($entity instanceof Entity)) {
0 ignored issues
show
introduced by
$entity is always a sub-type of AbterPhp\Contact\Domain\Entities\Form.
Loading history...
131
            throw new \InvalidArgumentException(__CLASS__ . ':' . __FUNCTION__ . ' expects a Form entity.');
132
        }
133
134
        $query = (new QueryBuilder())
135
            ->update(
136
                'contact_forms',
137
                'contact_forms',
138
                [
139
                    'name'       => [$entity->getName(), \PDO::PARAM_STR],
140
                    'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR],
141
                    'to_name'    => [$entity->getToName(), \PDO::PARAM_STR],
142
                    'to_email'   => [$entity->getToEmail(), \PDO::PARAM_STR],
143
                ]
144
            )
145
            ->where('id = ?')
146
            ->andWhere('deleted = 0')
147
            ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR);
148
149
        $sql    = $query->getSql();
150
        $params = $query->getParameters();
151
152
        $statement = $this->writeConnection->prepare($sql);
153
        $statement->bindValues($params);
154
        $statement->execute();
155
    }
156
157
    /**
158
     * @param array $hash
159
     *
160
     * @return Entity
161
     */
162
    protected function loadEntity(array $hash)
163
    {
164
        return new Entity(
165
            $hash['id'],
166
            $hash['name'],
167
            $hash['identifier'],
168
            $hash['to_name'],
169
            $hash['to_email']
170
        );
171
    }
172
173
    /**
174
     * @return SelectQuery
175
     */
176
    private function getBaseQuery(): SelectQuery
177
    {
178
        /** @var SelectQuery $query */
179
        $query = (new QueryBuilder())
180
            ->select(
181
                'cf.id',
182
                'cf.name',
183
                'cf.identifier',
184
                'cf.to_name',
185
                'cf.to_email'
186
            )
187
            ->from('contact_forms', 'cf')
188
            ->where('cf.deleted = 0');
189
190
        return $query;
191
    }
192
}
193