PathGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
dl 0
loc 38
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 27 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 01.01.2018
6
 * Time: 13:34
7
 */
8
9
namespace floor12\files\logic;
10
11
12
use yii\base\ErrorException;
13
14
class PathGenerator
15
{
16
17
    private $path = '';
18
19
20
    public function __construct($storagePath)
21
    {
22
23
        if (!$storagePath)
24
            throw new ErrorException('Storage path not set for path generator.');
25
26
        if (!file_exists($storagePath))
27
            mkdir($storagePath);
28
29
        if (!file_exists($storagePath))
30
            throw new ErrorException('Unable to create storage.');
31
32
        $folderName0 = rand(10, 99);
33
        $folderName1 = rand(10, 99);
34
35
        $path0 = DIRECTORY_SEPARATOR . $folderName0;
36
        $path1 = DIRECTORY_SEPARATOR . $folderName0 . DIRECTORY_SEPARATOR . $folderName1;
37
38
        $fullPath0 = $storagePath . $path0;
39
        $fullPath1 = $storagePath . $path1;
40
41
        if (!file_exists($fullPath0))
42
            mkdir($fullPath0);
43
        if (!file_exists($fullPath1))
44
            mkdir($fullPath1);
45
46
        $this->path = $path1 . DIRECTORY_SEPARATOR . md5(rand(0, 1000) . time());
47
    }
48
49
    public function __toString()
50
    {
51
        return $this->path;
52
    }
53
}