Completed
Push — master ( a1590e...0af16c )
by Vladimir
02:39
created

Folder::__toString()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\System;
4
5
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
/**
9
 * All folder paths stored inside of this class will **not** have the ending DIRECTORY_SEPARATOR
10
 *
11
 * @package allejo\stakx\System
12
 */
13
class Folder
14
{
15
    protected $fs;
16
    protected $absolutePath;
17
    protected $targetDirectories;
18
19 3
    public function __construct ($folderPath)
20
    {
21 3
        $this->fs = new Filesystem();
22 3
        $this->targetDirectories = array();
23
24
        // Setup the absolute path to the directory
25 3
        if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR)
26
        {
27 3
            $this->absolutePath = $folderPath;
28
        }
29
        else
30
        {
31
            $this->absolutePath = $this->fs->absolutePath($folderPath);
32
        }
33
34
        // Ensure our directory paths will don't end with a '/'
35 3
        $this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR);
36
37 3
        if (!$this->fs->isDir($this->absolutePath))
38
        {
39
            throw new \InvalidArgumentException();
40
        }
41
42 3
        if (!$this->fs->exists($this->absolutePath))
43
        {
44
            throw new FileNotFoundException();
45
        }
46 3
    }
47
48
    public function __toString ()
49
    {
50
        return rtrim($this->absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
51
    }
52
53
    public function setTargetDirectory ($folderName)
54
    {
55
        if (is_null($folderName) || empty($folderName))
56
        {
57
            $this->targetDirectories = array();
58
        }
59
        else
60
        {
61
            $this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR);
62
        }
63
    }
64
65
    /**
66
     * @param string $absolutePath
67
     * @param string $targetPath
68
     */
69
    public function copyFile ($absolutePath, $targetPath)
70
    {
71
        $targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR);
72
73
        $this->fs->copy(
74
            $absolutePath,
75
            $this->buildPath($this->getCwd(), $targetPath),
76
            true
77
        );
78
    }
79
80
    /**
81
     * @param string $targetPath
82
     * @param string $fileContent
83
     */
84 3 View Code Duplication
    public function writeFile ($targetPath, $fileContent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
85
    {
86 3
        $outputFolder   = $this->fs->getFolderPath($targetPath);
87 3
        $targetFileName = $this->fs->getFileName($outputFolder);
88
89 3
        $absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder);
90
91 3
        if (!file_exists($absoluteFolderPath))
92
        {
93 3
            mkdir($absoluteFolderPath, 0755, true);
94
        }
95
96 3
        file_put_contents(
97 3
            $this->buildPath($this->getCwd(), $targetPath),
98
            $fileContent,
99 3
            LOCK_EX
100
        );
101
102 3
        return (new SplFileInfo(
103
            $targetFileName,
104
            $absoluteFolderPath,
105 3
            $this->buildPath($absoluteFolderPath, $targetFileName)
106
        ));
107
    }
108
109
    /**
110
     * @param string $pathFragments
111
     */
112 3
    private function buildPath ($pathFragments)
0 ignored issues
show
Unused Code introduced by
The parameter $pathFragments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114 3
        $paths = func_get_args();
115
116 3
        return implode(DIRECTORY_SEPARATOR, $paths);
117
    }
118
119
    /**
120
     * Returns the absolute path of where files will be placed
121
     *
122
     * @return string
123
     */
124 3
    private function getCwd ()
125
    {
126 3
        $location = array_merge(array($this->absolutePath), $this->targetDirectories);
127
128 3
        return implode(DIRECTORY_SEPARATOR, $location);
129
    }
130
}