Completed
Push — master ( 519f76...318dd5 )
by Raffael
24:13 queued 15:29
created

Pdo::count()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 12
cp 0
rs 9.552
c 0
b 0
f 0
cc 3
nc 6
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint;
13
14
use Generator;
15
use Psr\Log\LoggerInterface;
16
use Tubee\AttributeMap\AttributeMapInterface;
17
use Tubee\Collection\CollectionInterface;
18
use Tubee\Endpoint\Pdo\Exception\InvalidQuery;
19
use Tubee\Endpoint\Pdo\Wrapper as PdoWrapper;
20
use Tubee\EndpointObject\EndpointObjectInterface;
21
use Tubee\Workflow\Factory as WorkflowFactory;
22
23
class Pdo extends AbstractSqlDatabase
24
{
25
    /**
26
     * Kind.
27
     */
28
    public const KIND = 'PdoEndpoint';
29
30
    /**
31
     * Init endpoint.
32
     */
33 5
    public function __construct(string $name, string $type, string $table, PdoWrapper $socket, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = [])
34
    {
35 5
        $this->socket = $socket;
36 5
        $this->table = $table;
37 5
        parent::__construct($name, $type, $collection, $workflow, $logger, $resource);
38 5
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function count(?array $query = null): int
44
    {
45
        list($filter, $values) = $this->transformQuery($query);
46
47
        if ($filter === null) {
48
            $sql = 'SELECT COUNT(*) as count FROM '.$this->table;
49
        } else {
50
            $sql = 'SELECT COUNT(*) as count FROM '.$this->table.' WHERE '.$filter;
51
        }
52
53
        try {
54
            $result = $this->socket->prepareValues($sql, $values);
55
56
            return (int) $result->fetch(\PDO::FETCH_ASSOC)['count'];
57
        } catch (InvalidQuery $e) {
58
            $this->logger->error('failed to count number of objects from endpoint', [
59
                'class' => get_class($this),
60
                'exception' => $e,
61
            ]);
62
63
            return 0;
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getAll(?array $query = null): Generator
71
    {
72
        list($filter, $values) = $this->transformQuery($query);
73
        $this->logGetAll($filter);
74
75
        if ($filter === null) {
76
            $sql = 'SELECT * FROM '.$this->table;
77
        } else {
78
            $sql = 'SELECT * FROM '.$this->table.' WHERE '.$filter;
79
        }
80
81
        try {
82
            $result = $this->socket->prepareValues($sql, $values);
83
        } catch (InvalidQuery $e) {
84
            $this->logger->error('failed to fetch resources from endpoint', [
85
                'class' => get_class($this),
86
                'exception' => $e,
87
            ]);
88
89
            return 0;
90
        }
91
92
        $i = 0;
93
        while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
94
            yield $this->build($row);
95
            ++$i;
96
        }
97
98
        return $i;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getOne(array $object, array $attributes = []): EndpointObjectInterface
105
    {
106
        list($filter, $values) = $query = $this->transformQuery($this->getFilterOne($object));
107
        $this->logGetOne($filter);
108
        $sql = 'SELECT * FROM '.$this->table.' WHERE '.$filter;
109
        $result = $this->socket->prepareValues($sql, $values);
110
        $row = $result->fetch(\PDO::FETCH_ASSOC);
111
        $seccond = $result->fetch(\PDO::FETCH_ASSOC);
112
113
        if ($seccond !== false) {
114
            throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter);
115
        }
116
        if ($row === false) {
117
            throw new Exception\ObjectNotFound('no object found with filter '.$filter);
118
        }
119
120
        return $this->build($row, $query);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function create(AttributeMapInterface $map, array $object, bool $simulate = false): ?string
127
    {
128
        $this->logCreate($object);
129
        $result = $this->prepareCreate($object, $simulate);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
131
        if ($simulate === true) {
132
            return null;
133
        }
134
135
        return $this->socket->lastInsertId();
0 ignored issues
show
Bug introduced by
The method lastInsertId does only exist in Tubee\Endpoint\Pdo\Wrapper, but not in Tubee\Endpoint\Mysql\Wrapper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
136
    }
137
}
138