PackageAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B getSlice() 0 22 5
1
<?php
2
3
/**
4
 * This file is part of the PHP SDK library for the Superdesk Content API.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace Superdesk\ContentApiSdk\Api\Pagerfanta;
16
17
use Superdesk\ContentApiSdk\Api\Request\RequestInterface;
18
use Superdesk\ContentApiSdk\Client\ApiClientInterface;
19
use Superdesk\ContentApiSdk\ContentApiSdk;
20
use Superdesk\ContentApiSdk\Data\Package;
21
use Superdesk\ContentApiSdk\Exception\InvalidDataException;
22
use Exception;
23
24
/**
25
 * Adapter for package.
26
 */
27
class PackageAdapter extends ResourceAdapter
28
{
29
    /**
30
     * SDK Instance.
31
     *
32
     * @var ContentApiSdk
33
     */
34
    protected $apiInstance;
35
36
    /**
37
     * Resolve package associations.
38
     *
39
     * @var boolean
40
     */
41
    protected $resolveAssociations;
42
43
    /**
44
     * Instantiate object.
45
     *
46
     * @param ApiClientInterface $client HTTP client
47
     * @param RequestInterface $request API Request is_object(var)
48
     * @param ContentApiSdk $apiInstance SDK Instance
49
     * @param boolean $resolveAssociations Resolve package associations
50
     */
51
    public function __construct(
52
        ApiClientInterface $client,
53
        RequestInterface $request,
54
        ContentApiSdk $apiInstance,
55
        $resolveAssociations
56
    ) {
57
        parent::__construct($client, $request);
58
59
        $this->apiInstance = $apiInstance;
60
        $this->resolveAssociations = $resolveAssociations;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getSlice($offset, $length)
67
    {
68
        $packages = array();
69
        $resources = parent::getSlice($offset, $length);
70
71
        try {
72
            foreach ($resources as $itemData) {
0 ignored issues
show
Bug introduced by
The expression $resources of type array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
73
                $packages[] = new Package($itemData);
74
            }
75
76
            if ($this->resolveAssociations) {
77
                foreach ($packages as $id => $package) {
78
                    $associations = $this->apiInstance->getAssociationsFromPackage($package);
79
                    $packages[$id] = $this->apiInstance->injectAssociations($package, $associations);
80
                }
81
            }
82
        } catch (Exception $e) {
83
            throw new InvalidDataException('Could not convert resources to packages.', $e->getCode(), $e);
84
        }
85
86
        return $packages;
87
    }
88
}
89