Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

BlockCache   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
c 1
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hasAnyChangedSince() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Databases\Queries;
6
7
use AbterPhp\Admin\Exception\Database;
8
use Opulence\Databases\ConnectionPools\ConnectionPool;
9
use Opulence\QueryBuilders\Conditions\ConditionFactory;
10
use Opulence\QueryBuilders\MySql\QueryBuilder;
11
12
/** @phan-file-suppress PhanTypeMismatchArgument */
13
14
class BlockCache
15
{
16
    /** @var ConnectionPool */
17
    protected $connectionPool;
18
19
    /**
20
     * BlockCache constructor.
21
     *
22
     * @param ConnectionPool $connectionPool
23
     */
24
    public function __construct(ConnectionPool $connectionPool)
25
    {
26
        $this->connectionPool = $connectionPool;
27
    }
28
29
    /**
30
     * @param string[] $identifiers
31
     * @param string   $cacheTime
32
     *
33
     * @return bool
34
     * @throws \Opulence\QueryBuilders\InvalidQueryException
35
     */
36
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
37
    {
38
        $conditions = new ConditionFactory();
39
        $query      = (new QueryBuilder())
40
            ->select('COUNT(*) AS count')
41
            ->from('blocks')
42
            ->leftJoin('block_layouts', 'layouts', 'layouts.id = blocks.layout_id')
43
            ->where('blocks.deleted_at IS NULL')
44
            ->andWhere($conditions->in('blocks.identifier', $identifiers))
45
            ->andWhere('blocks.updated_at > ? OR layouts.updated_at > ?')
46
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR)
47
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR);
48
49
        $connection = $this->connectionPool->getReadConnection();
50
        $statement  = $connection->prepare($query->getSql());
51
        $statement->bindValues($query->getParameters());
52
        if (!$statement->execute()) {
53
            throw new Database($statement->errorInfo());
54
        }
55
56
        return (int)$statement->fetchColumn() > 0;
57
    }
58
}
59