ApiResultLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 47
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 16 3
1
<?php
2
/**
3
 * Class ApiResultLoader
4
 */
5
namespace HDNET\OnpageIntegration\Loader;
6
7
use HDNET\OnpageIntegration\Exception\ApiErrorException;
8
use HDNET\OnpageIntegration\Persister\ApiResultToCachePersister;
9
use HDNET\OnpageIntegration\Service\DataService;
10
use TYPO3\CMS\Core\Cache\CacheManager;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
13
/**
14
 * Class ApiResultLoader
15
 */
16
class ApiResultLoader
17
{
18
19
    /**
20
     * @var \HDNET\OnpageIntegration\Service\DataService
21
     */
22
    protected $dataService;
23
24
    /**
25
     * @var \HDNET\OnpageIntegration\Persister\ApiResultToCachePersister
26
     */
27
    protected $persister;
28
29
    /**
30
     * ApiResultLoader constructor.
31
     *
32
     * @param \HDNET\OnpageIntegration\Service\DataService               $dataService
33
     * @param \HDNET\OnpageIntegration\Persister\ApiResultToCachePersister $apiResultToCachePersister
34
     */
35
    public function __construct(DataService $dataService, ApiResultToCachePersister $apiResultToCachePersister)
36
    {
37
        $this->dataService = $dataService;
38
        $this->persister = $apiResultToCachePersister;
39
    }
40
41
    /**
42
     * @param string $key
43
     *
44
     * @return array
45
     */
46
    public function load($key)
47
    {
48
        $cacheId = $this->persister->getIdentifier($key);
49
        $entry = GeneralUtility::makeInstance(CacheManager::class)
50
            ->getCache('onpage_extension')
51
            ->get($cacheId);
52
53
        if ($entry === false) {
54
            try {
55
                $entry = $this->dataService->getApiResult($key);
56
            } catch (ApiErrorException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
57
            }
58
            $this->persister->persist($entry, $key);
0 ignored issues
show
Documentation introduced by
$entry is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        }
60
        return json_decode($entry, true);
61
    }
62
}
63