| Conditions | 3 |
| Paths | 3 |
| Total Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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) { |
||
| 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 |