Completed
Pull Request — master (#16)
by Hannes
05:53
created

AbstractStorage::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6667
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <https://github.com/baleen/migrations>.
18
 */
19
20
namespace Baleen\Migrations\Storage;
21
22
use Baleen\Migrations\Exception\StorageException;
23
use Baleen\Migrations\Version\Collection\Migrated;
24
use Baleen\Migrations\Version\VersionInterface;
25
26
/**
27
 * Class AbstractStorage.
28
 *
29
 * @author Gabriel Somoza <[email protected]>
30
 */
31
abstract class AbstractStorage implements StorageInterface
32
{
33
    /**
34
     * Reads versions from the storage file.
35
     *
36
     * @return Migrated
37
     *
38
     * @throws StorageException
39
     */
40 6
    final public function fetchAll()
41
    {
42 6
        $collection = $this->doFetchAll();
43 6
        if (!$collection instanceof Migrated) {
44
            StorageException::throwInvalidObjectException($collection, Migrated::class);
45
        }
46
        return $collection;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    final public function update(VersionInterface $version)
53
    {
54
        if ($version->isMigrated()) {
55
            $result = $this->save($version);
56
        } else {
57
            $result = $this->delete($version);
58
        }
59
        return $result;
60
    }
61
62
    /**
63
     * @return Migrated
64
     */
65
    abstract protected function doFetchAll();
66
}
67