Completed
Push — master ( 300f7c...ac8da7 )
by Joschi
06:40
created

RepositoryIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 84
wmc 7
lcom 1
cbo 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 16 2
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Dev\Application\Service
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Dev\Application\Service;
38
39
use Apparat\Dev\Ports\Repository;
40
41
/**
42
 * Repository iterator
43
 *
44
 * @package Apparat\Dev
45
 * @subpackage Apparat\Dev\Application
46
 */
47
class RepositoryIterator implements RepositoryIteratorInterface
48
{
49
    /**
50
     * Object configuration
51
     *
52
     * @var array
53
     */
54
    protected $config;
55
    /**
56
     * Object stac
57
     *
58
     * @var array
59
     */
60
    protected $objectStack = [];
61
    /**
62
     * Day pointer
63
     *
64
     * @var \DateTime
65
     */
66
    protected $dayPointer;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param array $config Object configuration
72
     */
73
    public function __construct(array $config)
74
    {
75
        $this->config = $config;
76
    }
77
78
    /**
79
     * Return the current object config
80
     *
81
     * @return array Object config
82
     */
83
    public function current()
84
    {
85
        return $this->config[array_shift($this->objectStack)];
86
    }
87
88
    /**
89
     * Forward the day pointer by a random period between 0 and 259200 seconds (= 3 days)
90
     */
91
    public function next()
92
    {
93
        $this->dayPointer->add(new \DateInterval('PT'.rand(0, 259200).'S'));
94
    }
95
96
    /**
97
     * Return the day pointer
98
     *
99
     * @return \DateTime Day pointer
100
     */
101
    public function key()
102
    {
103
        return $this->dayPointer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->dayPointer; (DateTime) is incompatible with the return type declared by the interface Iterator::key of type integer|double|string|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
104
    }
105
106
    public function valid()
107
    {
108
        return count($this->objectStack) > 0;
109
    }
110
111
    /**
112
     * Rewind the iterator and rebuild the object stack
113
     */
114
    public function rewind()
115
    {
116
        // Recreate a random object stack
117
        $this->objectStack = [];
118
        foreach ($this->config as $objectType => $objectConfig) {
119
            $this->objectStack = array_pad(
120
                $this->objectStack,
121
                count($this->objectStack) + $objectConfig[Repository::COUNT],
122
                $objectType
123
            );
124
        }
125
        shuffle($this->objectStack);
126
127
        // Reset the day pointer
128
        $this->dayPointer = new \DateTime();
129
    }
130
}