Issues (157)

src/Blocks/FileManager.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use Illuminate\Console\Command;
6
use SoliDry\Exceptions\DirectoryException;
7
use SoliDry\Helpers\ConfigHelper;
8
use SoliDry\ApiGenerator;
9
use SoliDry\Types\ConsoleInterface;
10
use SoliDry\Types\DirsInterface;
11
use SoliDry\Types\ModulesInterface;
12
use SoliDry\Types\PhpInterface;
13
14
class FileManager implements DirsInterface
15
{
16
    public const FILE_MODE_CREATE = 'wb';
17
    public const DIR_MODE         = 0755;
18
19
    /**
20
     * @param string $fileName
21
     * @param string $content
22
     * @param bool   $isNew
23
     *
24
     * @return bool
25
     */
26
    public static function createFile($fileName, $content, $isNew = false): bool
27
    {
28
        if($isNew === true || file_exists($fileName) === false)
29
        {
30
            $fp = fopen($fileName, self::FILE_MODE_CREATE);
31
            fwrite($fp, $content);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
            fwrite(/** @scrutinizer ignore-type */ $fp, $content);
Loading history...
32
33
            return fclose($fp);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            return fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * @param string $path
41
     *
42
     * @throws DirectoryException
43
     */
44
    public static function createPath($path)
45
    {
46
        if(is_dir($path) === false)
47
        {
48
            if(mkdir($path, self::DIR_MODE, true) === false)
49
            {
50
                throw new DirectoryException(
51
                    'Couldn`t create directory '
52
                    . $path
53
                    . ' with '
54
                    . self::DIR_MODE
55
                    . ' mode.'
56
                );
57
            }
58
        }
59
    }
60
61
    /**
62
     * Constructs base module path
63
     *
64
     * @param Command $obj
65
     *
66
     * @param bool    $http
67
     *
68
     * @return string
69
     */
70
    public static function getModulePath(Command $obj, bool $http = false) : string
71
    {
72
        /** @var ApiGenerator $obj */
73
        $path = $obj->modulesDir . PhpInterface::SLASH . strtoupper($obj->version) . PhpInterface::SLASH;
74
75
        if($http === true)
76
        {
77
            $path .= $obj->httpDir . PhpInterface::SLASH;
78
        }
79
80
        return $path;
81
    }
82
83
    /**
84
     * Creates Modules config file
85
     *
86
     * @param string $sourceCode
87
     */
88
    public static function createModuleConfig(string $sourceCode)
89
    {
90
        self::createFile(
91
            DirsInterface::CONFIG_DIR . PhpInterface::SLASH . ModulesInterface::KEY_MODULE . PhpInterface::PHP_EXT, $sourceCode
92
        );
93
    }
94
95
    /**
96
     * @param array $options containing array of input options
97
     *
98
     * @return bool             true if option --regenerate is on, false otherwise
99
     */
100
    public static function isRegenerated(array $options): bool
101
    {
102
        return empty($options[ConsoleInterface::OPTION_REGENERATE]) ? false : true;
103
    }
104
105
    /**
106
     * @param Command $obj           generator object
107
     * @param string  $migrationName the name of a migration file
108
     *
109
     * @return bool                 true if migration with similar name exists, false otherwise
110
     */
111
    public static function migrationNotExists(Command $obj, string $migrationName): bool
112
    {
113
        $path  = FileManager::getModulePath($obj) . self::DATABASE_DIR . PhpInterface::SLASH
114
                 . $obj->migrationsDir . PhpInterface::SLASH;
115
        $file  = $path . PhpInterface::ASTERISK . $migrationName
116
                 . PhpInterface::PHP_EXT;
117
        $files = glob($file);
118
119
        return empty($files) ? true : false;
120
    }
121
122
    /**
123
     * Glues 2 entities in one pivot
124
     *
125
     * @param string $firstEntity
126
     * @param string $secondEntity
127
     *
128
     * @return string
129
     */
130
    public static function getPivotFile(string $firstEntity, string $secondEntity): string
131
    {
132
        return DirsInterface::MODULES_DIR . PhpInterface::SLASH
133
               . ConfigHelper::getModuleName() . PhpInterface::SLASH .
134
               DirsInterface::ENTITIES_DIR . PhpInterface::SLASH .
135
               $firstEntity . $secondEntity . PhpInterface::PHP_EXT;
136
    }
137
}