Completed
Push — master ( cc1638...e9bda5 )
by Wachter
02:21
created

ArrayTaskExecutionRepository::findByTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\Storage\ArrayStorage;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Task\Execution\TaskExecution;
17
use Task\Execution\TaskExecutionInterface;
18
use Task\Storage\TaskExecutionRepositoryInterface;
19
use Task\TaskInterface;
20
use Task\TaskStatus;
21
22
/**
23
 * Storage task-execution in an array.
24
 */
25
class ArrayTaskExecutionRepository implements TaskExecutionRepositoryInterface
26
{
27
    /**
28
     * @var Collection
29
     */
30
    private $taskExecutionCollection;
31
32
    /**
33
     * @param Collection $taskExecutions
34
     */
35 9
    public function __construct(Collection $taskExecutions = null)
36
    {
37 9
        $this->taskExecutionCollection = $taskExecutions ?: new ArrayCollection();
38 9
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function create(TaskInterface $task, \DateTime $scheduleTime)
44
    {
45
        return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload());
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function persist(TaskExecutionInterface $execution)
52
    {
53 1
        $this->taskExecutionCollection->add($execution);
54
55 1
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function remove(TaskExecutionInterface $execution)
62
    {
63 1
        $this->taskExecutionCollection->removeElement($execution);
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function flush()
72
    {
73 1
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
80
    {
81 2
        $filtered = $this->taskExecutionCollection->filter(
82
            function (TaskExecutionInterface $execution) use ($task, $scheduleTime) {
83 2
                return $execution->getTask()->getUuid() === $task->getUuid()
84 2
                    && $execution->getScheduleTime() === $scheduleTime
85 2
                    && $execution->getStatus() === TaskStatus::PLANNED;
86
            }
87 2
        );
88
89 2
        if ($filtered->count() === 0) {
90 1
            return;
91
        }
92
93 1
        return $filtered->first();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function findByTask(TaskInterface $task)
100
    {
101 1
        return array_values($this->taskExecutionCollection->filter(
102
            function (TaskExecutionInterface $execution) use ($task) {
103 1
                return $execution->getTask()->getUuid() === $task->getUuid();
104
            }
105 1
        )->toArray());
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function findAll($page = 1, $pageSize = null)
112
    {
113 2
        return array_values($this->taskExecutionCollection->slice(($page - 1) * $pageSize, $pageSize));
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    public function findScheduled()
120
    {
121 1
        $dateTime = new \DateTime();
122
123 1
        return $this->taskExecutionCollection->filter(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->taskExecut...Time() < $dateTime; }); (Doctrine\Common\Collections\Collection) is incompatible with the return type declared by the interface Task\Storage\TaskExecuti...nterface::findScheduled of type Task\Execution\TaskExecutionInterface[].

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...
124 1
            function (TaskExecutionInterface $execution) use ($dateTime) {
125 1
                return $execution->getStatus() === TaskStatus::PLANNED && $execution->getScheduleTime() < $dateTime;
126
            }
127 1
        );
128
    }
129
}
130