BlockCache   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAnyChangedSince() 0 22 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\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
class BlockCache
14
{
15
    /** @var ConnectionPool */
16
    protected $connectionPool;
17
18
    /**
19
     * BlockCache constructor.
20
     *
21
     * @param ConnectionPool $connectionPool
22
     */
23
    public function __construct(ConnectionPool $connectionPool)
24
    {
25
        $this->connectionPool = $connectionPool;
26
    }
27
28
    /**
29
     * @param string[] $identifiers
30
     * @param string   $cacheTime
31
     *
32
     * @return bool
33
     * @throws \Opulence\QueryBuilders\InvalidQueryException
34
     */
35
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
36
    {
37
        $conditions = new ConditionFactory();
38
        $query      = (new QueryBuilder())
39
            ->select('COUNT(*) AS count')
40
            ->from('blocks')
41
            ->leftJoin('block_layouts', 'block_layouts', 'block_layouts.id = blocks.layout_id')
42
            ->where('blocks.deleted_at IS NULL')
43
            ->andWhere($conditions->in('blocks.identifier', $identifiers))
44
            ->andWhere('blocks.updated_at > ? OR block_layouts.updated_at > ?')
45
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR)
46
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR)
47
        ;
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 $statement->fetchColumn() > 0;
57
    }
58
}
59