Completed
Pull Request — master (#51)
by Tim
09:44
created

AbstractCallback::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\AbstractCallback
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
25
/**
26
 * An abstract callback implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
abstract class AbstractCallback implements CallbackInterface
35
{
36
37
    /**
38
     * The observer's subject instance.
39
     *
40
     * @var \TechDivision\Import\Subjects\SubjectInterface
41
     */
42
    protected $subject;
43
44
    /**
45
     * Initializes the observer with the passed subject instance.
46
     *
47
     * @param \TechDivision\Import\Subjects\SubjectInterface|null $subject The observer's subject instance
48
     */
49
    public function __construct(SubjectInterface $subject = null)
50
    {
51
        if ($subject != null) {
52
            $this->setSubject($subject);
53
        }
54
    }
55
56
    /**
57
     * Set's the obeserver's subject instance to initialize the observer with.
58
     *
59
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject
60
     *
61
     * @return void
62
     */
63
    public function setSubject(SubjectInterface $subject)
64
    {
65
        $this->subject = $subject;
66
    }
67
68
    /**
69
     * Return's the observer's subject instance.
70
     *
71
     * @return \TechDivision\Import\Subjects\SubjectInterface The observer's subject instance
72
     */
73
    protected function getSubject()
74
    {
75
        return $this->subject;
76
    }
77
78
    /**
79
     * Return's the system logger.
80
     *
81
     * @return \Psr\Log\LoggerInterface The system logger instance
82
     */
83
    protected function getSystemLogger()
84
    {
85
        return $this->getSubject()->getSystemLogger();
86
    }
87
88
    /**
89
     * Append's the exception suffix containing filename and line number to the
90
     * passed message. If no message has been passed, only the suffix will be
91
     * returned.
92
     *
93
     * @param string|null $message    The message to append the exception suffix to
94
     * @param string|null $filename   The filename used to create the suffix
95
     * @param string|null $lineNumber The line number used to create the suffx
96
     *
97
     * @return string The message with the appended exception suffix
98
     */
99
    protected function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null)
100
    {
101
        return $this->getSubject()-> appendExceptionSuffix($message, $filename, $lineNumber);
102
    }
103
104
    /**
105
     * Wraps the passed exeception into a new one by trying to resolve the original filname,
106
     * line number and column name and use it for a detailed exception message.
107
     *
108
     * @param string     $columnName The column name that should be resolved
109
     * @param \Exception $parent     The exception we want to wrap
110
     * @param string     $className  The class name of the exception type we want to wrap the parent one
111
     *
112
     * @return \Exception the wrapped exception
113
     */
114
    protected function wrapException(
115
        $columnName,
116
        \Exception $parent = null,
117
        $className = '\TechDivision\Import\Exceptions\WrappedColumnException'
118
    ) {
119
        return $this->getSubject()->wrapException($columnName, $parent, $className);
120
    }
121
122
    /**
123
     * Queries whether or not debug mode is enabled or not, default is TRUE.
124
     *
125
     * @return boolean TRUE if debug mode is enabled, else FALSE
126
     */
127
    protected function isDebugMode()
128
    {
129
        return $this->getSubject()->isDebugMode();
130
    }
131
132
    /**
133
     * Merge the passed array into the status of the actual import.
134
     *
135
     * @param array $status The status information to be merged
136
     *
137
     * @return void
138
     */
139
    protected function mergeAttributesRecursive(array $status)
140
    {
141
142
        // load the subject instance
143
        $subject = $this->getSubject();
144
145
        // merge the passed status
146
        $subject->getRegistryProcessor()->mergeAttributesRecursive(
147
            $subject->getSerial(),
148
            $status
149
        );
150
    }
151
}
152