Completed
Push — master ( 52b305...ed76d4 )
by Guillermo A.
01:49
created

AbstractResource::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Guillermoandrae\Highrise\Resources;
4
5
use Guillermoandrae\Common\CollectionInterface;
6
use Guillermoandrae\Highrise\Helpers\Xml;
7
use Guillermoandrae\Highrise\Http\AdapterAwareTrait;
8
use Guillermoandrae\Highrise\Http\AdapterInterface;
9
use ICanBoogie\Inflector;
10
11
abstract class AbstractResource implements ResourceInterface
12
{
13
    use AdapterAwareTrait;
14
15
    /**
16
     * The name to be used in the resource endpoint.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * Builds the resource object.
24
     *
25
     * @param AdapterInterface $adapter  The HTTP adapter.
26
     */
27 23
    public function __construct(AdapterInterface $adapter)
28
    {
29 23
        $this->setAdapter($adapter);
30 23
        if (!$this->name) {
31 15
            $this->name = strtolower(
32 15
                str_replace(__NAMESPACE__ . '\\', '', get_class($this))
33
            );
34
        }
35 23
    }
36
37 1
    public function find($id): array
38
    {
39 1
        $uri = sprintf('/%s/%s.xml', $this->getName(), $id);
40 1
        return $this->getAdapter()->request('GET', $uri);
41
    }
42
43 1
    public function findAll(array $filters = []): CollectionInterface
44
    {
45 1
        $uri = sprintf('/%s.xml', $this->getName());
46 1
        return $this->getAdapter()->request('GET', $uri, ['query' => $filters]);
47
    }
48
49 2
    public function search(array $criteria = []): CollectionInterface
50
    {
51 2
        $uri = sprintf('/%s/search.xml', $this->getName());
52 2
        $query = [];
53 2
        if (isset($criteria['term'])) {
54 1
            $query['term'] = $criteria['term'];
55
        } else {
56 1
            foreach ($criteria as $key => $value) {
57 1
                $query[sprintf('criteria[%s]', $key)] = $value;
58
            }
59
        }
60 2
        return $this->getAdapter()->request('GET', $uri, ['query' => $query]);
61
    }
62
63 1
    public function create(array $data): array
64
    {
65 1
        $uri = sprintf('/%s.xml', $this->getName());
66 1
        $body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data);
67 1
        return $this->getAdapter()->request('POST', $uri, ['body' => $body]);
68
    }
69
70 1
    public function update($id, array $data): array
71
    {
72 1
        $uri = sprintf('/%s/%s.xml?reload=true', $this->getName(), $id);
73 1
        $body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data);
74 1
        return $this->getAdapter()->request('PUT', $uri, ['body' => $body]);
75
    }
76
77 1
    public function delete($id): bool
78
    {
79 1
        $uri = sprintf('/%s/%s.xml', $this->getName(), $id);
80 1
        return $this->getAdapter()->request('DELETE', $uri);
81
    }
82
83 17
    public function getName(): string
84
    {
85 17
        return $this->name;
86
    }
87
}
88