Completed
Pull Request — master (#48)
by Robbie
01:21
created

ScheduledRun::getTypeInstance()   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
2
/**
3
 * This is a simple object to hold different sets of settings for a scheduled run.
4
 *
5
 * The number of settings and details will be extended later on.
6
 */
7
class ScheduledRun extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var array
11
     */
12
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
        'Type' => 'Varchar(128)',
14
    );
15
16
    /**
17
     * Triggered by ScheduledExecutionJob this will add another job for the actual work.
18
     *
19
     * This "forking" of another process simply happens to make sure the scheduled process doesn't
20
     * get delayed further and further because of the actual execution time.
21
     */
22
    public function onScheduledExecution()
23
    {
24
        $type = $this->getTypeInstance();
25
        if ($type && $type->hasMethod('getJobName')) {
26
            // find which job needs to be added
27
            $jobName = $type->getJobName();
28
            Injector::inst()->get(QueuedJobService::class)->queueJob(
29
                Injector::inst()->create($jobName)
30
            );
31
        }
32
    }
33
34
    /**
35
     * Gets a singleton instance of the job, referred to as "Type"
36
     *
37
     * @return DataObject|null
38
     */
39
    public function getTypeInstance()
40
    {
41
        return Injector::inst()->get($this->Type);
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<ScheduledRun>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
    }
43
44
    /**
45
     * delivers the title for the job queue
46
     *
47
     * @return string
48
     */
49
    public function getTitle()
50
    {
51
        return singleton($this->Type)->singular_name();
0 ignored issues
show
Documentation introduced by
The property Type does not exist on object<ScheduledRun>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
    }
53
}
54