FilesystemStorage::write()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 5
Ratio 23.81 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 5
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
cc 4
eloc 11
nc 5
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Filesystem;
13
14
use Puli\Manager\Api\FileNotFoundException;
15
use Puli\Manager\Api\Storage\ReadException;
16
use Puli\Manager\Api\Storage\Storage;
17
use Puli\Manager\Assert\Assert;
18
use Symfony\Component\Filesystem\Filesystem;
19
use Webmozart\PathUtil\Path;
20
21
/**
22
 * Stores files on the filesystem.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class FilesystemStorage implements Storage
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 55
    public function read($path)
34
    {
35 55
        Assert::notEmpty($path, 'Cannot read an empty path.');
36
37 52
        if (!file_exists($path)) {
38 3
            throw new FileNotFoundException(sprintf('Cannot read %s: File not found.', $path));
39
        }
40
41 50
        if (is_dir($path)) {
42 1
            throw new ReadException(sprintf('Cannot read %s: Is a directory.', $path));
43
        }
44
45 49 View Code Duplication
        if (false === ($contents = @file_get_contents($path))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46 1
            $error = error_get_last();
47
48 1
            throw new ReadException(sprintf('Could not read %s: %s.', $path, $error['message']));
49
        }
50
51 48
        return $contents;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 7
    public function write($path, $contents)
58
    {
59 7
        Assert::notEmpty($path, 'Cannot write to an empty path.');
60
61 4
        if (is_dir($path)) {
62 1
            throw new ReadException(sprintf('Cannot write %s: Is a directory.', $path));
63
        }
64
65 3
        if (!is_dir($dir = Path::getDirectory($path))) {
66 1
            $filesystem = new Filesystem();
67 1
            $filesystem->mkdir($dir);
68
        }
69
70 3 View Code Duplication
        if (false === ($numBytes = @file_put_contents($path, $contents))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71 1
            $error = error_get_last();
72
73 1
            throw new ReadException(sprintf('Could not write %s: %s.', $path, $error['message']));
74
        }
75
76 2
        return $numBytes;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function exists($path)
83
    {
84 1
        return file_exists($path);
85
    }
86
}
87