Completed
Branch v4 (4e54dd)
by Pieter
03:26
created

FrameworkLessConnection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getOverviewUrlForResourceClass() 0 16 3
A getService() 0 3 1
A getBaseUrl() 0 6 2
A getExampleUrl() 0 7 2
A getAcceptLanguage() 0 3 1
A getUrlForResource() 0 11 3
A getContentLanguage() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
4
namespace W2w\Lib\Apie\Core\Bridge;
5
6
use W2w\Lib\Apie\Apie;
7
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest;
8
use W2w\Lib\Apie\Exceptions\BadConfigurationException;
9
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface;
10
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait;
11
use W2w\Lib\Apie\PluginInterfaces\FrameworkConnectionInterface;
12
13
class FrameworkLessConnection implements FrameworkConnectionInterface, ApieAwareInterface
14
{
15
    use ApieAwareTrait;
16
17
    public function __construct(Apie $apie)
18
    {
19
        $this->setApie($apie);
20
    }
21
22
    public function getService(string $id): object
23
    {
24
        throw new BadConfigurationException('No service ' . $id . ' found!');
25
    }
26
27
    public function getUrlForResource(object $resource): ?string
28
    {
29
        $classResourceConverter = $this->getApie()->getClassResourceConverter();
30
        $identifierExtractor = $this->getApie()->getIdentifierExtractor();
31
        $apiMetadataFactory = $this->getApie()->getApiResourceMetadataFactory();
32
        $metadata = $apiMetadataFactory->getMetadata($resource);
33
        $identifier = $identifierExtractor->getIdentifierValue($resource, $metadata->getContext());
34
        if (!$identifier || !$metadata->allowGet()) {
35
            return null;
36
        }
37
        return $this->getBaseUrl() . '/' . $classResourceConverter->normalize($metadata->getClassName()) . '/' . $identifier;
38
    }
39
40
    public function getExampleUrl(string $resourceClass): ?string
41
    {
42
        $url = $this->getOverviewUrlForResourceClass($resourceClass);
43
        if (null === $url) {
44
            return null;
45
        }
46
        return $url . '/12345';
47
    }
48
49
    public function getOverviewUrlForResourceClass(string $resourceClass, ?SearchFilterRequest $filterRequest = null
50
    ): ?string {
51
        $classResourceConverter = $this->getApie()->getClassResourceConverter();
52
        $apiMetadataFactory = $this->getApie()->getApiResourceMetadataFactory();
53
        $metadata = $apiMetadataFactory->getMetadata($resourceClass);
54
        if (!$metadata->allowGetAll()) {
55
            return null;
56
        }
57
        $query = '';
58
        if ($filterRequest) {
59
            $searchQuery = $filterRequest->getSearches();
60
            $searchQuery['page'] = $filterRequest->getPageIndex();
61
            $searchQuery['limit'] = $filterRequest->getNumberOfItems();
62
            $query = '?' . http_build_query($searchQuery);
63
        }
64
        return $this->getBaseUrl() . '/' . $classResourceConverter->normalize($metadata->getClassName()) . $query;
65
    }
66
67
    /**
68
     * Returns base url if one is set up.
69
     *
70
     * @return string
71
     */
72
    private function getBaseUrl(): string
73
    {
74
        try {
75
            return $this->getApie()->getBaseUrl();
76
        } catch (BadConfigurationException $exception) {
77
            return '';
78
        }
79
    }
80
81
    public function getAcceptLanguage(): ?string
82
    {
83
        return null;
84
    }
85
86
    public function getContentLanguage(): ?string
87
    {
88
        return null;
89
    }
90
}
91