Completed
Pull Request — master (#2)
by Beñat
02:35
created

InMemoryStorage::findAll()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Admin\Storage;
13
14
/**
15
 * In memory strategy of storage.
16
 *
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class InMemoryStorage implements Storage
20
{
21
    /**
22
     * The data collection.
23
     *
24
     * @var array
25
     */
26
    private $data;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param mixed $data The data
32
     */
33
    public function __construct($data)
34
    {
35
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 View Code Duplication
    public function findAll($limit, $offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $data = $this->data;
44
        foreach ($data as $key => $item) {
45
            if (array_key_exists('ID', $item)) {
46
                continue;
47
            }
48
            $data[$key]['ID'] = $key;
49
        }
50
        usort($data, [$this, 'sort']);
51
52
        return $this->paginate($data, $limit, $offset);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 View Code Duplication
    public function query(array $criteria, $limit, $offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $data = [];
61
        foreach ($this->data as $key => $item) {
62
            foreach ($criteria as $name => $singleCriteria) {
63
                if ($item[$name] === $singleCriteria) {
64
                    $data[$key] = $item;
65
                }
66
            }
67
            if (array_key_exists('ID', $item)) {
68
                continue;
69
            }
70
            $data[$key]['ID'] = $key;
71
        }
72
        usort($data, [$this, 'sort']);
73
74
        return $this->paginate($data, $limit, $offset);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function properties()
81
    {
82
        if (is_array($this->data[0])) {
83
            return array_keys($this->data[0]);
84
        }
85
86
        return array_keys($this->data);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function size()
93
    {
94
        return count($this->data);
95
    }
96
97
    /**
98
     * Paginates the given collection of data.
99
     *
100
     * @param array $data   The collection data
101
     * @param int   $limit  Logs per page
102
     * @param int   $offset The number of the page
103
     *
104
     * @return array
105
     */
106
    private function paginate($data, $limit, $offset)
107
    {
108
        return array_slice($data, (($offset - 1) * $limit), $limit);
109
    }
110
111
    /**
112
     * Compares the given two items.
113
     *
114
     * @param mixed $item1 The first item
115
     * @param mixed $item2 The second item
116
     *
117
     * @return int
118
     */
119 View Code Duplication
    private function sort($item1, $item2)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
sort uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
120
    {
121
        $orderBy = 'name';
122
        $order = 'asc';
123
124
        if (!empty($_GET['orderby'])) {
125
            $orderBy = $_GET['orderby'];
126
        }
127
        if (!empty($_GET['order'])) {
128
            $order = $_GET['order'];
129
        }
130
        $result = strcmp($item1[$orderBy], $item2[$orderBy]);
131
        if ('asc' === $order) {
132
            return $result;
133
        }
134
135
        return -$result;
136
    }
137
}
138