Completed
Push — master ( 8deccf...0fd7bd )
by
unknown
03:42 queued 02:08
created

SolrReindexQueuedJobBase::getSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SilverStripe\FullTextSearch\Solr\Reindex\Jobs;
4
5
use Monolog\Logger;
6
use Psr\Log\LoggerInterface;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler;
9
use SilverStripe\FullTextSearch\Utils\Logging\SearchLogFactory;
10
use stdClass;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
13
if (!interface_exists(QueuedJob::class)) {
14
    return;
15
}
16
17
/**
18
 * Base class for jobs which perform re-index
19
 */
20
abstract class SolrReindexQueuedJobBase implements QueuedJob
21
{
22
    /**
23
     * Flag whether this job is done
24
     *
25
     * @var bool
26
     */
27
    protected $isComplete;
28
29
    /**
30
     * List of messages
31
     *
32
     * @var array
33
     */
34
    protected $messages;
35
36
    /**
37
     * Logger to use for this job
38
     *
39
     * @var LoggerInterface
40
     */
41
    protected $logger;
42
43
    public function __construct()
44
    {
45
        $this->isComplete = false;
46
        $this->messages = array();
47
    }
48
49
    /**
50
     * @return SearchLogFactory
51
     */
52
    protected function getLoggerFactory()
53
    {
54
        return Injector::inst()->get(SearchLogFactory::class);
55
    }
56
57
    /**
58
     * Gets a logger for this job
59
     *
60
     * @return LoggerInterface
61
     */
62
    protected function getLogger()
63
    {
64
        if ($this->logger) {
65
            return $this->logger;
66
        }
67
68
        // Set logger for this job
69
        $this->logger = $this
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getLoggerFactory(...tQueuedJobLogger($this) of type object<Psr\Log> is incompatible with the declared type object<Psr\Log\LoggerInterface> of property $logger.

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...
70
            ->getLoggerFactory()
71
            ->getQueuedJobLogger($this);
72
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->logger; (Psr\Log) is incompatible with the return type documented by SilverStripe\FullTextSea...ueuedJobBase::getLogger of type Psr\Log\LoggerInterface.

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...
73
    }
74
75
    /**
76
     * Assign custom logger for this job
77
     *
78
     * @param LoggerInterface $logger
79
     */
80
    public function setLogger($logger)
81
    {
82
        $this->logger = $logger;
83
    }
84
85
    public function getJobData()
86
    {
87
        $data = new stdClass();
88
89
        // Standard fields
90
        $data->totalSteps = 1;
91
        $data->currentStep = $this->isComplete ? 0 : 1;
92
        $data->isComplete = $this->isComplete;
93
        $data->messages = $this->messages;
94
95
        // Custom data
96
        $data->jobData = new stdClass();
97
        return $data;
98
    }
99
100
    public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages)
101
    {
102
        $this->isComplete = $isComplete;
103
        $this->messages = $messages;
104
    }
105
106
    /**
107
     * Get the reindex handler
108
     *
109
     * @return SolrReindexHandler
110
     */
111
    protected function getHandler()
112
    {
113
        return Injector::inst()->get(SolrReindexHandler::class);
114
    }
115
116
    public function jobFinished()
117
    {
118
        return $this->isComplete;
119
    }
120
121
    public function prepareForRestart()
122
    {
123
        // NOOP
124
    }
125
126
    public function setup()
127
    {
128
        // NOOP
129
    }
130
131
    public function afterComplete()
132
    {
133
        // NOOP
134
    }
135
136
    public function getJobType()
137
    {
138
        return QueuedJob::QUEUED;
139
    }
140
141
    public function getSignature()
142
    {
143
        return sha1(get_class($this) . time() . mt_rand(0, 100000));
144
    }
145
146
    public function addMessage($message)
147
    {
148
        $this->messages[] = $message;
149
    }
150
}
151