Completed
Push — 16.x ( b566e8...a832f7 )
by Tim
02:53
created

RowTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 0
dl 0
loc 152
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A skipRow() 0 11 2
A rowHasToBeSkipped() 0 4 1
A setRow() 0 4 1
A getRow() 0 4 1
A hasValue() 0 15 3
A setValue() 0 4 1
B getValue() 0 29 8
1
<?php
2
3
/**
4
 * TechDivision\Import\RowTrait
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;
22
23
/**
24
 * A trait that provides row handling.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
trait RowTrait
33
{
34
35
    /**
36
     * The actual row, that has to be processed.
37
     *
38
     * @var array
39
     */
40
    protected $row = array();
41
42
    /**
43
     * The flag that stop's overserver execution on the actual row.
44
     *
45
     * @var boolean
46
     */
47
    protected $skipRow = false;
48
49
    /**
50
     * The number of rows that has been skipped.
51
     *
52
     * @var integer
53
     */
54
    private $skippedRows = 0;
55
56
    /**
57
     * Stop's observer execution on the actual row.
58
     *
59
     * @param boolean $skip The flag to skip row exection or not
60
     *
61
     * @return void
62
     */
63 1
    public function skipRow($skip = true)
64
    {
65
66
        // raise the counter for the skipped rows
67 1
        if ($skip === true) {
68 1
            $this->skippedRows++;
69
        }
70
71
        // set the the flag to signal the row has been skipped
72 1
        $this->skipRow = $skip;
73 1
    }
74
75
    /**
76
     * Query's whether or not the observer execution for the given row has to be skipped.
77
     *
78
     * @return boolean TRUE if the observer execution has to be skipped, else FALSE
79
     */
80
    public function rowHasToBeSkipped()
81
    {
82
        return $this->skipRow;
83
    }
84
85
    /**
86
     * Set's the actual row, that has to be processed.
87
     *
88
     * @param array $row The row
89
     *
90
     * @return void
91
     */
92 10
    public function setRow(array $row)
93
    {
94 10
        $this->row = $row;
95 10
    }
96
97
    /**
98
     * Return's the actual row.
99
     *
100
     * @return array The actual row
101
     */
102 14
    public function getRow()
103
    {
104 14
        return $this->row;
105
    }
106
107
    /**
108
     * Query whether or not a value for the column with the passed name exists.
109
     *
110
     * @param string $name The column name to query for a valid value
111
     *
112
     * @return boolean TRUE if the value is set, else FALSE
113
     */
114 2
    public function hasValue($name)
115
    {
116
117
        // query whether or not the header is available
118 2
        if ($this->hasHeader($name)) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
            // load the key for the row
120 1
            $headerValue = $this->getHeader($name);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
121
122
            // query whether the rows column has a vaild value
123 1
            return (isset($this->row[$headerValue]) && $this->row[$headerValue] !== '');
124
        }
125
126
        // return FALSE if not
127 1
        return false;
128
    }
129
130
    /**
131
     * Set the value in the passed column name.
132
     *
133
     * @param string $name  The column name to set the value for
134
     * @param mixed  $value The value to set
135
     *
136
     * @return void
137
     */
138 4
    protected function setValue($name, $value)
139
    {
140 4
        $this->row[$this->getHeader($name)] = $value;
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
141 4
    }
142
143
    /**
144
     * Resolve's the value with the passed colum name from the actual row. If a callback will
145
     * be passed, the callback will be invoked with the found value as parameter. If
146
     * the value is NULL or empty, the default value will be returned.
147
     *
148
     * @param string        $name     The name of the column to return the value for
149
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
150
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
151
     *
152
     * @return mixed|null The, almost formatted, value
153
     */
154 15
    public function getValue($name, $default = null, callable $callback = null)
155
    {
156
157
        // initialize the value
158 15
        $value = null;
159
160
        // query whether or not the header is available
161 15
        if ($this->hasHeader($name)) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
162
            // load the header value
163 7
            $headerValue = $this->getHeader($name);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
164
            // query wheter or not, the value with the requested key is available
165 7
            if ((isset($this->row[$headerValue]) && $this->row[$headerValue] !== '')) {
166 7
                $value = $this->row[$headerValue];
167
            }
168
        }
169
170
        // query whether or not, a callback has been passed
171 15
        if ($value != null && is_callable($callback)) {
172 1
            $value = call_user_func($callback, $value);
173
        }
174
175
        // query whether or not
176 15
        if ($value == null && $default !== null) {
177 1
            $value = $default;
178
        }
179
180
        // return the value
181 15
        return $value;
182
    }
183
}
184