ContentListCache   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hasAnyChangedSince() 0 20 2
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 ContentListCache
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('lists')
41
            ->where('lists.deleted_at IS NULL')
42
            ->andWhere($conditions->in('lists.identifier', $identifiers))
43
            ->andWhere('lists.updated_at > ?')
44
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR)
45
        ;
46
47
        $connection = $this->connectionPool->getReadConnection();
48
        $statement  = $connection->prepare($query->getSql());
49
        $statement->bindValues($query->getParameters());
50
        if (!$statement->execute()) {
51
            throw new Database($statement->errorInfo());
52
        }
53
54
        return $statement->fetchColumn() > 0;
55
    }
56
}
57