Completed
Push — develop ( f102ca...488b09 )
by Mathias
20:12 queued 11:00
created

LazyJob::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 8
nop 2
crap 20
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Queue;
12
13
use Core\Queue\Exception\AbstractJobException;
14
use Core\Queue\Exception\FatalJobException;
15
use SlmQueue\Job\AbstractJob;
16
use SlmQueue\Job\JobPluginManager;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class LazyJob extends AbstractJob
25
{
26
    protected $container;
27
28
    public function __construct(JobPluginManager $container, array $options = null)
29
    {
30
        $this->container = $container;
31
        if (isset($options['name'])) {
32
            $this->setServiceName($options['name']);
33
        }
34
        if (isset($options['options'])) {
35
            $this->setServiceOptions($options['options']);
36
        }
37
        if (isset($options['content'])) {
38
            $this->setContent($options['content']);
39
        }
40
    }
41
42
    public function setServiceName($name)
43
    {
44
        $this->setMetadata('service_name', $name);
45
    }
46
47
    public function getServiceName()
48
    {
49
        return $this->getMetadata('service_name');
50
    }
51
52
    public function setServiceOptions($options)
53
    {
54
        $this->setMetadata('service_options', $options);
55
    }
56
57
    public function getServiceOptions()
58
    {
59
        return $this->getMetadata('service_options');
60
    }
61
62
    public function execute()
63
    {
64
        $service = $this->getServiceName();
65
66
        if ($this->container->has($service)) {
67
            $job = $this->container->get($service, $this->getServiceOptions());
68
69
        } elseif (class_exists($service)) {
70
            $options = $this->getServiceOptions();
71
            $job = $options ? new $service(...$options) : new $service;
72
73
        } else {
74
            throw new FatalJobException('A job with name "' . $service . '" could not be created.');
75
        }
76
77
        $job->setContent($this->getContent());
78
        if ($job instanceOf LazyJobWrapperAwareInterface) {
0 ignored issues
show
Bug introduced by
The type Core\Queue\LazyJobWrapperAwareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
            $job->setWrapper($this);
0 ignored issues
show
Bug introduced by
The method setWrapper() does not exist on SlmQueue\Job\JobInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $job->/** @scrutinizer ignore-call */ 
80
                  setWrapper($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        }
81
82
        return $job->execute();
83
    }
84
85
}
86