Completed
Push — master ( a8f20d...0172d7 )
by Seth
01:51
created

AbstractWordPressSearchDomain::setApi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace smtech\StMarksSearch\WordPress;
4
5
use Battis\Educoder\Pest;
6
use smtech\StMarksSearch\AbstractSearchDomain;
7
use smtech\StMarksSearch\SearchResult;
8
use smtech\StMarksSearch\SearchSource;
9
10
/**
11
 * A parent object for WordPress search domains
12
 *
13
 * @author Seth Battis <[email protected]>
14
 */
15
abstract class AbstractWordPressSearchDomain extends AbstractSearchDomain
16
{
17
    /**
18
     * API access object
19
     * @var Pest
20
     */
21
    protected $api;
22
23
    /**
24
     * Summary information about search domain for search results
25
     * @var SearchSource
26
     */
27
    protected $source;
28
29
    /**
30
     * Construct a WordPress search domain: `$params` must contain a `url`
31
     * field with a valid URL to a WordPress blog
32
     *
33
     * @param mixed[] $params
34
     */
35
    public function __construct($params)
36
    {
37
        parent::__construct($params);
38
39
        $this->setApi($this->getUrl());
40
    }
41
42
    /**
43
     * Update the API access object
44
     *
45
     * @used-by AbstractWordPressSearchDomain::__construct()
46
     * @param string $url
47
     */
48
    protected function setApi($url)
49
    {
50
        if (!preg_match('%.*/wp-json/wp/v\d+$%', $url)) {
51
            $url = "$url/wp-json/wp/v2";
52
        }
53
        $this->api = new Pest($url);
54
    }
55
56
    /**
57
     * Get the API access object
58
     *
59
     * @return Pest
60
     */
61
    public function getApi()
62
    {
63
        return $this->api;
64
    }
65
66
    /**
67
     * Hook to convert individual items returned by the WordPress API into
68
     * SearchResults
69
     *
70
     * @used-by AbstractWordPressSearchDomain::processResponse()
71
     *
72
     * @param array $item JSON-decoded associative array representing a
73
     *                    WordPress object
74
     * @param string $query
75
     * @return SearchResult
76
     */
77
    abstract protected function processItem($item, $query);
78
79
    /**
80
     * Process the a response from the WordPress API listing objects
81
     *
82
     * @uses AbstractWordPressSearchDomain::processItem()
83
     *
84
     * @param string $response JSON-formatted WordPress api response
85
     * @param string $query
86
     * @return SearchResult[] Unordered
87
     */
88
    protected function processResponse($response, $query)
89
    {
90
        $results = [];
91
        do {
92
            $items = json_decode($response, true);
93
            foreach ($items as $item) {
94
                $results[] = $this->processItem($item, $query);
95
            }
96
97
            $link = $this->api->lastHeader('Link');
98
            if (preg_match('@<' . $this->api->base_url . '([^>]*)?(.*)>; rel="next"@', $link, $match)) {
99
                parse_str($match[2], $params);
100
                $response = $this->api->get($match[1], $params);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type null; however, Battis\Educoder\Pest::get() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
            }
102
        } while (!empty($match));
103
104
        return $results;
105
    }
106
}
107