Completed
Push — master ( 4142c5...662dc7 )
by Tim
12s
created

AbstractCallback::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 2
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
     * Raises the value for the counter with the passed key by one.
134
     *
135
     * @param mixed $counterName The name of the counter to raise
136
     *
137
     * @return integer The counter's new value
138
     */
139
    protected function raiseCounter($counterName)
140
    {
141
        return $this->getSubject()->raiseCounter($counterName);
142
    }
143
144
    /**
145
     * Merge the passed array into the status of the actual import.
146
     *
147
     * @param array $status The status information to be merged
148
     *
149
     * @return void
150
     */
151
    protected function mergeAttributesRecursive(array $status)
152
    {
153
        $this->getSubject()->mergeAttributesRecursive($status);
154
    }
155
156
    /**
157
     * Resolve's the value with the passed colum name from the actual row. If a callback will
158
     * be passed, the callback will be invoked with the found value as parameter. If
159
     * the value is NULL or empty, the default value will be returned.
160
     *
161
     * @param string        $name     The name of the column to return the value for
162
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
163
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
164
     *
165
     * @return mixed|null The, almost formatted, value
166
     */
167
    protected function getValue($name, $default = null, callable $callback = null)
168
    {
169
        return $this->getSubject()->getValue($name, $default, $callback);
170
    }
171
172
    /**
173
     * Return's the unique identifier of the actual row, e. g. a products SKU.
174
     *
175
     * @return mixed The row's unique identifier
176
     */
177
    abstract protected function getUniqueIdentifier();
178
}
179