Completed
Push — master ( 5a0d9f...69c9b8 )
by Caio
03:17
created

AbstractFilePathManager::parseOwnerClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
1
<?php
2
namespace CbCaio\ImgAttacher\Managers;
3
4
use CbCaio\ImgAttacher\Contracts\AttacherImageContract;
5
use CbCaio\ImgAttacher\Contracts\FilePathManagerContract;
6
use Illuminate\Database\Eloquent\Model;
7
8
abstract class AbstractFilePathManager implements FilePathManagerContract
9
{
10
    /** @var string */
11
    protected $path;
12
13
    /** @var string */
14
    protected $base_url;
15
16
    /** @var string */
17
    protected $processing_styles_routines;
18
19
    /** @var array */
20
    protected $arguments = [
21
        ':id'       => 'id',
22
        ':filename' => 'file_name'
23
    ];
24
25
    /**
26
     * @param string $path
27
     * @param string $base_url
28
     * @param array  $processing_styles_routines
29
     */
30
    public function __construct($path, $base_url, $processing_styles_routines)
31
    {
32
        $this->path              = $path;
33
        $this->base_url          = $base_url;
34
        $this->processing_styles = $processing_styles_routines;
0 ignored issues
show
Bug introduced by
The property processing_styles does not seem to exist. Did you mean processing_styles_routines?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35
    }
36
37
    /**
38
     * @param AttacherImageContract $attacherImage
39
     * @param string                $style
40
     * @return mixed|string
41
     */
42
    public function parsePath(AttacherImageContract $attacherImage, $style)
43
    {
44
        $string = $this->parseStyle($style, $this->getPath());
45
        $string = $this->parseOwnerClass($attacherImage, $string);
46
        $string = $this->parseAttributes($attacherImage, $string);
47
48
        return $string;
49
    }
50
51
    /**
52
     * @param AttacherImageContract $attacherImage
53
     * @param string                $string
54
     * @return mixed
55
     */
56
    public function parseOwnerClass(AttacherImageContract $attacherImage, $string)
57
    {
58
        if (!empty($attacherImage->getOwnerType()))
59
        {
60
            $owner_class_name = preg_split('/\\\\/', $attacherImage->getOwnerType())[1];
61
            $string           = preg_replace('/:owner_class\b/', $owner_class_name, $string);
62
        } else
63
        {
64
            return $string;
65
        }
66
67
        $model_owner = $attacherImage->owner()->getResults();
68
69
        if ($model_owner instanceof Model)
70
        {
71
            $string = preg_replace('/:owner_id\b/', $model_owner->id, $string);
72
        }
73
74
        return $string;
75
    }
76
77
    /**
78
     * @param AttacherImageContract $attacherImage
79
     * @param string                $string
80
     * @return mixed
81
     */
82
    public function parseAttributes(AttacherImageContract $attacherImage, $string)
83
    {
84
        foreach ($this->getArguments() as $key => $value)
85
        {
86
            if (strpos($string, $key) !== FALSE)
87
            {
88
                $string = preg_replace("/$key" . '\b/', $attacherImage->getAttribute($value), $string);
89
            }
90
        }
91
92
        return $string;
93
    }
94
95
    /**
96
     * @param string $style
97
     * @param string $string
98
     * @return string
99
     */
100
    public function parseStyle($style, $string)
101
    {
102
        foreach ($this->getProcessingStylesRoutines() as $processing_styles_routine => $styles_array)
103
        {
104
            if (array_has($styles_array, $style))
105
            {
106
                $string = preg_replace('/:style\b/', $style, $string);
107
            }
108
        }
109
110
        return $string;
111
    }
112
113
    /**
114
     * @param AttacherImageContract $attacherImage
115
     * @param string|null           $style
116
     * @return string
117
     */
118
    public function parseDeletePath(AttacherImageContract $attacherImage, $style = NULL)
119
    {
120
        is_null($style)
121
            ? preg_match('/.*:owner_class\/:owner_id\b/', $this->getPath(), $delete_path)
122
            : preg_match('/.*:owner_class\/:owner_id\/:style\b/', $this->getPath(), $delete_path);
123
124
125
        $delete_path = $this->parseStyle($style, $delete_path)[0];
0 ignored issues
show
Documentation introduced by
$delete_path is of type array<integer,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
        $delete_path = $this->parseOwnerClass($attacherImage, $delete_path);
127
        $delete_path = $this->parseAttributes($attacherImage, $delete_path);
128
129
        return $delete_path;
130
    }
131
132
    /**
133
     * @param AttacherImageContract $attacherImage
134
     * @param string                $style
135
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
136
     */
137
    public function parseUrl(AttacherImageContract $attacherImage, $style = NULL)
138
    {
139
        $url = $this->getBaseUrl() . $this->parsePath($attacherImage, $style);
140
        $url = preg_replace('~(^|[^:])//+~', '\\1/', $url);
141
142
        return url($url);
143
    }
144
145
    /**
146
     * @param AttacherImageContract $attacherImage
147
     * @return string|null
148
     */
149
    public function parsePreviousPath(AttacherImageContract $attacherImage)
150
    {
151
        return $attacherImage->hasDifferentFileName()
152
            ? $this->parseDeletePath($attacherImage, NULL)
153
            : NULL;
154
    }
155
156
    /** @return array */
157
    public function getArguments()
158
    {
159
        return $this->arguments;
160
    }
161
162
    /**
163
     * @param array $string
164
     * @return $this
165
     */
166
    public function setArguments($string)
167
    {
168
        $this->arguments = $string;
169
170
        return $this;
171
    }
172
173
    /** @return string */
174
    public function getBaseUrl()
175
    {
176
        return $this->base_url;
177
    }
178
179
    /** @return array */
180
    public function getProcessingStylesRoutines()
181
    {
182
        return $this->processing_styles;
0 ignored issues
show
Bug introduced by
The property processing_styles does not seem to exist. Did you mean processing_styles_routines?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
183
    }
184
185
    /** @return string */
186
    public function getPath()
187
    {
188
        return $this->path;
189
    }
190
191
}