BasePHPExecutor   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 89.66%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 62
dl 0
loc 112
ccs 52
cts 58
cp 0.8966
rs 10
c 3
b 1
f 0
wmc 28

4 Methods

Rating   Name   Duplication   Size   Complexity  
C setReferences() 0 41 16
A getArguments() 0 17 4
A handleException() 0 22 6
A runCallable() 0 14 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
6
use Kaliop\eZMigrationBundle\API\Exception\MigrationBundleException;
7
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
8
9
/**
10
 * @property ReferenceResolverBagInterface $referenceResolver
11
 */
12
abstract class BasePHPExecutor extends AbstractExecutor
13
{
14
    use IgnorableStepExecutorTrait;
0 ignored issues
show
Bug introduced by
The trait Kaliop\eZMigrationBundle...orableStepExecutorTrait requires the property $dsl which is not provided by Kaliop\eZMigrationBundle...xecutor\BasePHPExecutor.
Loading history...
15
    use ReferenceSetterTrait;
16
17
    /**
18 3
     * @param $result
19
     * @param \Exception|null $exception
20 3
     * @param $dsl
21
     * @return bool
22
     * @throws InvalidStepDefinitionException
23
     */
24
    protected function setReferences($result, \Exception $exception = null, $dsl)
25
    {
26 3
        if (!array_key_exists('references', $dsl) || !count($dsl['references'])) {
27
            return false;
28
        }
29
30
        foreach ($dsl['references'] as $key => $reference) {
31
            $reference = $this->parseReferenceDefinition($key, $reference);
32
            switch ($reference['attribute']) {
33
                case 'result':
34
                    if (!$this->isValidReferenceValue($result)) {
35
                        throw new MigrationBundleException("PHP executor can not set references for attribute 'result': it is not a scalar or array");
36
                    }
37 3
                    $value = $result;
38
                    break;
39 3
                case 'exception_code':
40 1
                    $value = $exception ? $exception->getCode() : null;
41
                    break;
42
                case 'exception_message':
43 2
                // BC
44 2
                case 'exception_text':
45 2
                    $value = $exception ? $exception->getMessage() : null;
46 2
                    break;
47 2
                case 'exception_file':
48 2
                    $value = $exception ? $exception->getFile() : null;
49 2
                    break;
50 1
                case 'exception_line':
51 1
                    $value = $exception ? $exception->getLine() : null;
52 2
                    break;
53
                default:
54 2
                    throw new InvalidStepDefinitionException('PHP executor does not support setting references for attribute ' . $reference['attribute']);
55 2
            }
56 2
57 1
            $overwrite = false;
58 1
            if (isset($reference['overwrite'])) {
59 1
                $overwrite = $reference['overwrite'];
60 1
            }
61 1
            $this->addReference($reference['identifier'], $value, $overwrite);
62 1
        }
63
64
        return true;
65
    }
66
67 2
    protected function getArguments($dsl)
68 2
    {
69
        if (isset($dsl['arguments'])) {
70
            if (!is_array($dsl['arguments'])) {
71 2
                throw new InvalidStepDefinitionException("'arguments' is not an array in php migration step");
72
            }
73
74 2
            $args = $dsl['arguments'];
75
76
            foreach ($args as &$val) {
77 4
                $val = $this->resolveReferencesRecursively($val);
78
            }
79 4
        } else {
80 3
            $args = array();
81
        }
82
83
        return $args;
84 3
    }
85
86 3
    protected function runCallable($callable, $args, $dsl)
87 3
    {
88
        $exception = null;
89
        $result = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
90 2
        try {
91
            $result = call_user_func_array($callable, $args);
92
        /// @todo catch \Throwable
93 4
        } catch (\Exception $exception) {
94
            $this->handleException($exception, $dsl);
95
        }
96 4
97
        $this->setReferences($result, $exception, $dsl);
98 4
99 4
        return $result;
100
    }
101 4
102 3
    protected function handleException($exception, $dsl)
103 3
    {
104
        $catch = false;
105
106 3
        // allow to specify a set of exceptions to tolerate
107
        if (isset($dsl['catch'])) {
108 3
            if (is_array($dsl['catch'])) {
109
                $caught = $dsl['catch'];
110
            } else {
111 3
                $caught = array($dsl['catch']);
112
            }
113 3
114
            foreach ($caught as $baseException) {
115
                if (is_a($exception, $baseException)) {
116 3
                    $catch = true;
117 2
                    break;
118 1
                }
119
            }
120 1
        }
121
122
        if (!$catch) {
123 2
            throw $exception;
124 2
        }
125 2
    }
126
}
127