RepositoryAbstract::whereId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by Maciej Paprocki for Bureau-VA.
4
 * Date: 17/02/2016
5
 * Project Name: MaciekPaprocki\WordpressGuzzle
6
 * Time: 10:46.
7
 */
8
namespace BureauVa\WordpressGuzzle\Query;
9
10
use BureauVa\WordpressGuzzle\Helper\StringHelper as S;
11
use Cache\Adapter\Common\AbstractCachePool as CachePool;
12
13
/**
14
 * Class AbstractRepository.
15
 */
16
abstract class RepositoryAbstract
17
{
18
    public static $BASE_PATH = 'posts';
19
20
    /**
21
     * @return string
22
     */
23 4
    public function __toString()
24
    {
25 4
        $ar = (array) $this;
26
27 4
        return $this->prepareCall($ar);
28
    }
29
30
    /**
31
     * @param $ar
32
     *
33
     * @return string
34
     */
35 4
    private function prepareCall($ar)
36
    {
37 4
        if (count($ar) === 1 && isset($ar['post__in']) && is_numeric($ar['post__in'])) {
38 4
            return self::$BASE_PATH.'/'.$ar['post__in'];
39
        }
40
41 4
        return self::$BASE_PATH.'?'.http_build_query([
42 4
            'filter' => $ar,
43 2
        ]);
44
    }
45
46
    /**
47
     * @param $method
48
     * @param $params
49
     *
50
     * @return $this
51
     */
52 2
    public function __call($method, $params)
53
    {
54 2
        if (substr($method, 0, 5) == 'where') {
55 2
            $varName = S::camelCaseToSnakeCase(mb_substr($method, 5));
56
57 2
            $this->$varName = $params[0];
58 1
        }
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * @param $id
65
     *
66
     * @return $this
67
     */
68 4
    public function whereId($id)
69
    {
70 4
        $this->post__in = (int) $id;
0 ignored issues
show
Bug introduced by
The property post__in does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
72 4
        return $this;
73
    }
74
75
    /**
76
     * Set's.
77
     *
78
     * @param $key
79
     * @param $value
80
     */
81
    public function setNotFiltered($key, $value)
82
    {
83
        $this->$key = $value;
84
85
        return $this;
86
    }
87
88 4
    public function whereIds($ids)
89
    {
90 4
        $this->post__in = (array) $ids;
91
92 4
        return $this;
93
    }
94
95
    /**
96
     * Checks if there is any cache data for query.
97
     *
98
     * @param CachePool $cachePool
0 ignored issues
show
Bug introduced by
There is no parameter named $cachePool. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
     */
100 2
    public function getCacheKey()
101
    {
102
        //TODO: implement real cache check, no it uses not so smart method of caching by query address
103 2
        return (string) $this;
104
    }
105
}
106