Completed
Push — master ( 6d3173...686ecf )
by Guillermo A.
09:20
created

AbstractResource   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 6 1
A search() 0 13 3
A find() 0 5 1
A getName() 0 3 1
A findAll() 0 5 1
A create() 0 6 1
A __construct() 0 6 2
A hydrate() 0 14 4
A delete() 0 4 1
1
<?php
2
3
namespace Guillermoandrae\Highrise\Resources;
4
5
use Guillermoandrae\Common\Collection;
6
use Guillermoandrae\Common\CollectionInterface;
7
use Guillermoandrae\Highrise\Entities\EntityFactory;
8
use Guillermoandrae\Highrise\Entities\EntityInterface;
9
use Guillermoandrae\Highrise\Helpers\Xml;
10
use Guillermoandrae\Highrise\Http\AdapterAwareTrait;
11
use Guillermoandrae\Highrise\Http\AdapterInterface;
12
use ICanBoogie\Inflector;
13
14
abstract class AbstractResource implements ResourceInterface
15
{
16
    use AdapterAwareTrait;
17
18
    /**
19
     * The name to be used in the resource endpoint.
20
     *
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * Builds the resource object.
27 24
     *
28
     * @param AdapterInterface $adapter  The HTTP adapter.
29 24
     */
30 24
    public function __construct(AdapterInterface $adapter)
31 16
    {
32 16
        $this->setAdapter($adapter);
33
        if (!$this->name) {
34
            $this->name = strtolower(
35 24
                str_replace(__NAMESPACE__ . '\\', '', get_class($this))
36
            );
37 1
        }
38
    }
39 1
40 1
    public function find($id): EntityInterface
41
    {
42
        $uri = sprintf('/%s/%s.xml', $this->getName(), $id);
43 1
        $results = $this->getAdapter()->request('GET', $uri);
44
        return $this->hydrate($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hydrate($results) also could return the type Guillermoandrae\Common\Collection|array which is incompatible with the return type mandated by Guillermoandrae\Highrise...sourceInterface::find() of Guillermoandrae\Highrise\Entities\EntityInterface.
Loading history...
45 1
    }
46 1
47
    public function findAll(array $filters = []): CollectionInterface
48
    {
49 2
        $uri = sprintf('/%s.xml', $this->getName());
50
        $results = $this->getAdapter()->request('GET', $uri, ['query' => $filters]);
51 2
        return $this->hydrate($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hydrate($results) could return the type array which is incompatible with the type-hinted return Guillermoandrae\Common\CollectionInterface. Consider adding an additional type-check to rule them out.
Loading history...
52 2
    }
53 2
54 1
    public function search(array $criteria = []): CollectionInterface
55
    {
56 1
        $uri = sprintf('/%s/search.xml', $this->getName());
57 1
        $query = [];
58
        if (isset($criteria['term'])) {
59
            $query['term'] = $criteria['term'];
60 2
        } else {
61
            foreach ($criteria as $key => $value) {
62
                $query[sprintf('criteria[%s]', $key)] = $value;
63 1
            }
64
        }
65 1
        $results = $this->getAdapter()->request('GET', $uri, ['query' => $query]);
66 1
        return $this->hydrate($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hydrate($results) could return the type array which is incompatible with the type-hinted return Guillermoandrae\Common\CollectionInterface. Consider adding an additional type-check to rule them out.
Loading history...
67 1
68
    }
69
70 1
    public function create(array $data): EntityInterface
71
    {
72 1
        $uri = sprintf('/%s.xml', $this->getName());
73 1
        $body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data);
74 1
        $results = $this->getAdapter()->request('POST', $uri, ['body' => $body]);
75
        return $this->hydrate($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hydrate($results) also could return the type Guillermoandrae\Common\Collection|array which is incompatible with the return type mandated by Guillermoandrae\Highrise...urceInterface::create() of Guillermoandrae\Highrise\Entities\EntityInterface.
Loading history...
76
77 1
    }
78
79 1
    public function update($id, array $data): EntityInterface
80 1
    {
81
        $uri = sprintf('/%s/%s.xml?reload=true', $this->getName(), $id);
82
        $body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data);
83 18
        $results = $this->getAdapter()->request('PUT', $uri, ['body' => $body]);
84
        return $this->hydrate($results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hydrate($results) also could return the type Guillermoandrae\Common\Collection|array which is incompatible with the return type mandated by Guillermoandrae\Highrise...urceInterface::update() of Guillermoandrae\Highrise\Entities\EntityInterface.
Loading history...
85 18
86
    }
87
88
    public function delete($id): bool
89
    {
90
        $uri = sprintf('/%s/%s.xml', $this->getName(), $id);
91
        return $this->getAdapter()->request('DELETE', $uri);
92
    }
93
94
    public function getName(): string
95
    {
96
        return $this->name;
97
    }
98
99
    protected function hydrate(string $xml)
100
    {
101
        libxml_use_internal_errors(true);
102
        if (!$obj = simplexml_load_string($xml)) {
103
            return [];
104
        }
105
        if ($obj->attributes()) {
106
            $items = [];
107
            foreach ($obj->children() as $child) {
108
                $items[] = $this->hydrate($child);
109
            }
110
            return Collection::make($items);
111
        }
112
        return EntityFactory::factory($this->getName(), $xml);
113
    }
114
}
115