Completed
Push — features/addons-api ( a15d66 )
by Sam
10:47
created

AddonsApiController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 66
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 49 3
1
<?php
2
3
class AddonsApiController extends ApiController
4
{
5
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
6
        'index',
7
    ];
8
9
    /**
10
     * Set the default cache lifetime in seconds. Only used outside of "dev" environments.
11
     *
12
     * Set to 1 week
13
     *
14
     * @config
15
     * @var int
16
     */
17
    private static $cache_age = 86400;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
19
    public function index(SS_HTTPRequest $request)
20
    {
21
22
        $after = (int)$request->getVar('after');
23
        $limit = 50;
24
25
        // Sort by ID. This will keep pagination consisent even if new records are added
26
        // Pagination is based on records greater than a given ID to ensure this is true
27
        // if records are deleted
28
        $addons = Addon::get()->sort('ID');
29
        $pageAddons = $addons->filter('ID:greaterthan', $after)->limit($limit)->toArray();
30
31
        if ($pageAddons) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageAddons of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
33
            $converter = Injector::inst()->get('AddonToArray');
34
            $apiAddons = array_map(
35
                function ($addon) use ($converter) {
36
                    return $converter->convert($addon);
37
                },
38
                $pageAddons
39
            );
40
41
            $maxID = max(array_map(
42
                function ($addon) {
43
                    return $addon->ID;
44
                },
45
                $pageAddons
46
            ));
47
48
            $result = [
49
                'success' => true,
50
                'addons' => $apiAddons
51
            ];
52
53
            if ($addons->filter('ID:greaterthan', $maxID)->count() > 0) {
54
                $result['next'] = Director::absoluteURL(HTTP::setGetVar('after', $maxID));
55
            }
56
57
        } else {
58
            $result = [
59
                'success' => false,
60
                'message' => 'No addons found',
61
            ];
62
        }
63
64
65
66
        return $this->formatResponse($result);
67
    }
68
}
69