Completed
Push — master ( 0e9bc5...7ef25f )
by Ryan
15:00
created

Image::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Image;
2
3
use Anomaly\FilesModule\File\Contract\FileInterface;
4
use Anomaly\FilesModule\File\FilePresenter;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
6
use Anomaly\Streams\Platform\Application\Application;
7
use Anomaly\Streams\Platform\Routing\UrlGenerator;
8
use Collective\Html\HtmlBuilder;
9
use Illuminate\Contracts\Config\Repository;
10
use Illuminate\Filesystem\Filesystem;
11
use Intervention\Image\Constraint;
12
use Intervention\Image\ImageManager;
13
use League\Flysystem\File;
14
use League\Flysystem\MountManager;
15
use Mobile_Detect;
16
use Robbo\Presenter\Presenter;
17
18
/**
19
 * Class Image
20
 *
21
 * @link    http://pyrocms.com/
22
 * @author  PyroCMS, Inc. <[email protected]>
23
 * @author  Ryan Thompson <[email protected]>
24
 */
25
class Image
26
{
27
28
    /**
29
     * The publish flag.
30
     *
31
     * @var bool
32
     */
33
    protected $publish = false;
34
35
    /**
36
     * The publishable base directory.
37
     *
38
     * @var null
39
     */
40
    protected $directory = null;
41
42
    /**
43
     * The image object.
44
     *
45
     * @var null|string
46
     */
47
    protected $image = null;
48
49
    /**
50
     * The file extension.
51
     *
52
     * @var null|string
53
     */
54
    protected $extension = null;
55
56
    /**
57
     * The desired filename.
58
     *
59
     * @var null|string
60
     */
61
    protected $filename = null;
62
63
    /**
64
     * The version flag.
65
     *
66
     * @var null|boolean
67
     */
68
    protected $version = null;
69
70
    /**
71
     * The default output method.
72
     *
73
     * @var string
74
     */
75
    protected $output = 'url';
76
77
    /**
78
     * The image attributes.
79
     *
80
     * @var array
81
     */
82
    protected $attributes = [];
83
84
    /**
85
     * Applied alterations.
86
     *
87
     * @var array
88
     */
89
    protected $alterations = [];
90
91
    /**
92
     * Image srcsets.
93
     *
94
     * @var array
95
     */
96
    protected $srcsets = [];
97
98
    /**
99
     * Image sources.
100
     *
101
     * @var array
102
     */
103
    protected $sources = [];
104
105
    /**
106
     * Allowed methods.
107
     *
108
     * @var array
109
     */
110
    protected $allowedMethods = [
111
        'blur',
112
        'brightness',
113
        'colorize',
114
        'resizeCanvas',
115
        'contrast',
116
        'crop',
117
        'encode',
118
        'fit',
119
        'flip',
120
        'gamma',
121
        'greyscale',
122
        'heighten',
123
        'insert',
124
        'invert',
125
        'limitColors',
126
        'pixelate',
127
        'opacity',
128
        'resize',
129
        'rotate',
130
        'amount',
131
        'widen',
132
        'orientate',
133
    ];
134
135
    /**
136
     * The quality of the output.
137
     *
138
     * @var null|int
139
     */
140
    protected $quality = null;
141
142
    /**
143
     * The image width.
144
     *
145
     * @var null|int
146
     */
147
    protected $width = null;
148
149
    /**
150
     * The image height.
151
     *
152
     * @var null|int
153
     */
154
    protected $height = null;
155
156
    /**
157
     * The URL generator.
158
     *
159
     * @var UrlGenerator
160
     */
161
    protected $url;
162
163
    /**
164
     * The HTML builder.
165
     *
166
     * @var HtmlBuilder
167
     */
168
    protected $html;
169
170
    /**
171
     * Image path hints by namespace.
172
     *
173
     * @var ImagePaths
174
     */
175
    protected $paths;
176
177
    /**
178
     * The image macros.
179
     *
180
     * @var ImageMacros
181
     */
182
    protected $macros;
183
184
    /**
185
     * The file system.
186
     *
187
     * @var Filesystem
188
     */
189
    protected $files;
190
191
    /**
192
     * The user agent utility.
193
     *
194
     * @var Mobile_Detect
195
     */
196
    protected $agent;
197
198
    /**
199
     * The config repository.
200
     *
201
     * @var Repository
202
     */
203
    protected $config;
204
205
    /**
206
     * The image manager.
207
     *
208
     * @var ImageManager
209
     */
210
    protected $manager;
211
212
    /**
213
     * The stream application.
214
     *
215
     * @var Application
216
     */
217
    protected $application;
218
219
    /**
220
     * Create a new Image instance.
221
     *
222
     * @param UrlGenerator  $url
223
     * @param HtmlBuilder   $html
224
     * @param Filesystem    $files
225
     * @param Mobile_Detect $agent
226
     * @param Repository    $config
227
     * @param ImageManager  $manager
228
     * @param Application   $application
229
     * @param ImagePaths    $paths
230
     * @param ImageMacros   $macros
231
     */
232 View Code Duplication
    public function __construct(
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...
233
        UrlGenerator $url,
234
        HtmlBuilder $html,
235
        Filesystem $files,
236
        Mobile_Detect $agent,
237
        Repository $config,
238
        ImageManager $manager,
239
        Application $application,
240
        ImagePaths $paths,
241
        ImageMacros $macros
242
    ) {
243
        $this->url         = $url;
244
        $this->html        = $html;
245
        $this->files       = $files;
246
        $this->agent       = $agent;
247
        $this->paths       = $paths;
248
        $this->config      = $config;
249
        $this->macros      = $macros;
250
        $this->manager     = $manager;
251
        $this->application = $application;
252
    }
253
254
    /**
255
     * Make a new image instance.
256
     *
257
     * @param  mixed $image
258
     * @param  null  $output
259
     * @return $this
260
     */
261
    public function make($image, $output = null)
262
    {
263
        if ($image instanceof Image) {
264
            return $image;
265
        }
266
267
        if ($output) {
268
            $this->setOutput($output);
269
        }
270
271
        $clone = clone($this);
272
273
        $clone->setAlterations([]);
274
        $clone->setSources([]);
275
        $clone->setSrcsets([]);
276
        $clone->setImage(null);
277
278
        try {
279
            return $clone->setImage($image);
280
        } catch (\Exception $e) {
281
            return $this;
282
        }
283
    }
284
285
    /**
286
     * Return the path to an image.
287
     *
288
     * @return string
289
     */
290
    public function path()
291
    {
292
        $path = $this->getCachePath();
293
294
        return $path;
295
    }
296
297
    /**
298
     * Return the asset path to an image.
299
     *
300
     * @return string
301
     */
302
    public function asset()
303
    {
304
        $path = $this->getCachePath();
305
306
        return $this->url->asset($path);
307
    }
308
309
    /**
310
     * Run a macro on the image.
311
     *
312
     * @param $macro
313
     * @return Image
314
     * @throws \Exception
315
     */
316
    public function macro($macro)
317
    {
318
        return $this->macros->run($macro, $this);
319
    }
320
321
    /**
322
     * Return the URL to an image.
323
     *
324
     * @param  array $parameters
325
     * @param  null  $secure
326
     * @return string
327
     */
328
    public function url(array $parameters = [], $secure = null)
329
    {
330
        return $this->url->asset($this->path(), $parameters, $secure);
331
    }
332
333
    /**
334
     * Return the image tag to an image.
335
     *
336
     * @param  null  $alt
337
     * @param  array $attributes
338
     * @return string
339
     */
340
    public function image($alt = null, array $attributes = [])
341
    {
342
        $attributes = array_merge($this->getAttributes(), $attributes);
343
344
        $attributes['src'] = $this->asset();
345
346
        if ($srcset = $this->srcset()) {
347
            $attributes['srcset'] = $srcset;
348
        }
349
350
        if (!$alt && $this->config->get('streams::images.auto_alt', true)) {
351
352
            $attributes['alt'] = array_get(
353
                $this->getAttributes(),
354
                'alt',
355
                ucwords(
356
                    str_humanize(
357
                        trim(
358
                            basename(
359
                                str_contains($attributes['src'], '?v=') ?
360
                                    substr($attributes['src'], 0, strpos($attributes['src'], '?v=')) :
361
                                    $attributes['src'],
362
                                $this->getExtension()
363
                            ),
364
                            '.'
365
                        ),
366
                        '^a-zA-Z0-9'
367
                    )
368
                )
369
            );
370
        }
371
372
        return '<img ' . $this->html->attributes($attributes) . '>';
373
    }
374
375
    /**
376
     * Return the image tag to an image.
377
     *
378
     * @param  null  $alt
379
     * @param  array $attributes
380
     * @return string
381
     */
382
    public function img($alt = null, array $attributes = [])
383
    {
384
        return $this->image($alt, $attributes);
385
    }
386
387
    /**
388
     * Return a picture tag.
389
     *
390
     * @return string
391
     */
392
    public function picture(array $attributes = [])
393
    {
394
        $sources = [];
395
396
        $attributes = array_merge($this->getAttributes(), $attributes);
397
398
        /* @var Image $image */
399
        foreach ($this->getSources() as $media => $image) {
400
            if ($media != 'fallback') {
401
                $sources[] = $image->source();
402
            } else {
403
                $sources[] = $image->image();
404
            }
405
        }
406
407
        $sources = implode("\n", $sources);
408
409
        $attributes = $this->html->attributes($attributes);
410
411
        return "<picture {$attributes}>\n{$sources}\n</picture>";
412
    }
413
414
    /**
415
     * Return a source tag.
416
     *
417
     * @return string
418
     */
419
    public function source()
420
    {
421
        $this->addAttribute('srcset', $this->srcset() ?: $this->asset() . ' 2x, ' . $this->asset() . ' 1x');
422
423
        $attributes = $this->html->attributes($this->getAttributes());
424
425
        if ($srcset = $this->srcset()) {
426
            $attributes['srcset'] = $srcset;
427
        }
428
429
        return "<source {$attributes}>";
430
    }
431
432
    /**
433
     * Return the image response.
434
     *
435
     * @param  null $format
436
     * @param  int  $quality
437
     * @return String
438
     */
439
    public function encode($format = null, $quality = null)
440
    {
441
        return $this->manager->make($this->getCachePath())->encode($format, $quality ?: $this->getQuality());
442
    }
443
444
    /**
445
     * Return the image contents.
446
     *
447
     * @return string
448
     */
449
    public function data()
450
    {
451
        return $this->dumpImage();
452
    }
453
454
    /**
455
     * Return the output.
456
     *
457
     * @return string
458
     */
459
    public function output()
460
    {
461
        return $this->{$this->output}();
462
    }
463
464
    /**
465
     * Set the filename.
466
     *
467
     * @param $filename
468
     * @return $this
469
     */
470
    public function rename($filename = null)
471
    {
472
        return $this->setFilename($filename);
473
    }
474
475
    /**
476
     * Set the version flag.
477
     *
478
     * @param bool $version
479
     * @return $this
480
     */
481
    public function version($version = true)
482
    {
483
        return $this->setVersion($version);
484
    }
485
486
    /**
487
     * Set the quality.
488
     *
489
     * @param $quality
490
     * @return $this
491
     */
492
    public function quality($quality)
493
    {
494
        return $this->setQuality($quality);
495
    }
496
497
    /**
498
     * Set the width attribute.
499
     *
500
     * @param  null $width
501
     * @return Image
502
     */
503
    public function width($width = null)
504
    {
505
        return $this->addAttribute('width', $width ?: $this->getWidth());
506
    }
507
508
    /**
509
     * Set the height attribute.
510
     *
511
     * @param  null $height
512
     * @return Image
513
     */
514
    public function height($height = null)
515
    {
516
        return $this->addAttribute('height', $height ?: $this->getHeight());
517
    }
518
519
    /**
520
     * Set the quality.
521
     *
522
     * @param $quality
523
     * @return $this
524
     */
525
    public function setQuality($quality)
526
    {
527
        $this->quality = (int)$quality;
528
529
        return $this;
530
    }
531
532
    /**
533
     * Get the cache path of the image.
534
     *
535
     * @return string
536
     */
537
    protected function getCachePath()
538
    {
539
        if (starts_with($this->getImage(), ['http://', 'https://', '//'])) {
540
            return $this->getImage();
541
        }
542
543
        $path = $this->paths->outputPath($this);
544
545
        try {
546
            if ($this->shouldPublish($path)) {
547
                $this->publish($path);
548
            }
549
        } catch (\Exception $e) {
550
            return $this->config->get('app.debug', false) ? $e->getMessage() : null;
551
        }
552
553
        if ($this->config->get('streams::images.version') || $this->getVersion() == true) {
554
            $path .= '?v=' . filemtime(public_path(trim($path, '/\\')));
555
        }
556
557
        return $path;
558
    }
559
560
    /**
561
     * Determine if the image needs to be published
562
     *
563
     * @param $path
564
     * @return bool
565
     */
566
    private function shouldPublish($path)
567
    {
568
        $path = ltrim($path, '/');
569
570
        if (!$this->files->exists($path)) {
571
            return true;
572
        }
573
574 View Code Duplication
        if (is_string($this->image) && !str_is('*://*', $this->image) && filemtime($path) < filemtime($this->image)) {
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...
575
            return true;
576
        }
577
578 View Code Duplication
        if (is_string($this->image) && str_is('*://*', $this->image) && filemtime($path) < app(
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...
579
                'League\Flysystem\MountManager'
580
            )->getTimestamp($this->image)
581
        ) {
582
            return true;
583
        }
584
585
        if ($this->image instanceof File && filemtime($path) < $this->image->getTimestamp()) {
586
            return true;
587
        }
588
589
        if ($this->image instanceof FileInterface && filemtime($path) < $this->image->lastModified()->format('U')) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
590
            return true;
591
        }
592
593
        return false;
594
    }
595
596
    /**
597
     * Publish an image to the publish directory.
598
     *
599
     * @param $path
600
     */
601
    protected function publish($path)
602
    {
603
        $path = ltrim($path, '/');
604
605
        $this->files->makeDirectory((new \SplFileInfo($path))->getPath(), 0777, true, true);
606
607
        if (!$this->supportsType($this->getExtension())) {
608
609
            $this->files->put($path, $this->dumpImage());
610
611
            return;
612
        }
613
614
        if (!$image = $this->makeImage()) {
615
            return;
616
        }
617
618
        if (function_exists('exif_read_data') && $image->exif('Orientation') && $image->exif('Orientation') > 1) {
619
            $this->addAlteration('orientate');
620
        }
621
622
        if (!$this->getAlterations() && $content = $this->dumpImage()) {
623
            $this->files->put($this->directory . $path, $content);
624
625
            return;
626
        }
627
628
        if (is_callable('exif_read_data') && in_array('orientate', $this->getAlterations())) {
629
            $this->setAlterations(array_unique(array_merge(['orientate'], $this->getAlterations())));
630
        }
631
632
        foreach ($this->getAlterations() as $method => $arguments) {
633
            if ($method == 'resize') {
634
                $this->guessResizeArguments($arguments);
635
            }
636
637
            if (in_array($method, $this->getAllowedMethods())) {
638
                if (is_array($arguments)) {
639
                    call_user_func_array([$image, $method], $arguments);
640
                } else {
641
                    call_user_func([$image, $method], $arguments);
642
                }
643
            }
644
        }
645
646
        $image->save($this->directory . $path, $this->getQuality());
647
    }
648
649
    /**
650
     * Set an attribute value.
651
     *
652
     * @param $attribute
653
     * @param $value
654
     * @return $this
655
     */
656
    public function attr($attribute, $value)
657
    {
658
        array_set($this->attributes, $attribute, $value);
659
660
        return $this;
661
    }
662
663
    /**
664
     * Return the image srcsets by set.
665
     *
666
     * @return array
667
     */
668
    public function srcset()
669
    {
670
        $sources = [];
671
672
        /* @var Image $image */
673
        foreach ($this->getSrcsets() as $descriptor => $image) {
674
            $sources[] = $image->asset() . ' ' . $descriptor;
675
        }
676
677
        return implode(', ', $sources);
678
    }
679
680
    /**
681
     * Set the srcsets/alterations.
682
     *
683
     * @param array $srcsets
684
     */
685
    public function srcsets(array $srcsets)
686
    {
687
        foreach ($srcsets as $descriptor => &$alterations) {
688
            $image = $this->make(array_pull($alterations, 'image', $this->getImage()))->setOutput('url');
689
690 View Code Duplication
            foreach ($alterations as $method => $arguments) {
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...
691
                if (is_array($arguments)) {
692
                    call_user_func_array([$image, $method], $arguments);
693
                } else {
694
                    call_user_func([$image, $method], $arguments);
695
                }
696
            }
697
698
            $alterations = $image;
699
        }
700
701
        $this->setSrcsets($srcsets);
702
703
        return $this;
704
    }
705
706
    /**
707
     * Set the sources/alterations.
708
     *
709
     * @param  array $sources
710
     * @param  bool  $merge
711
     * @return $this
712
     */
713
    public function sources(array $sources, $merge = true)
714
    {
715
        foreach ($sources as $media => &$alterations) {
716
            if ($merge) {
717
                $alterations = array_merge($this->getAlterations(), $alterations);
718
            }
719
720
            $image = $this->make(array_pull($alterations, 'image', $this->getImage()))->setOutput('source');
721
722
            if ($media != 'fallback') {
723
                call_user_func([$image, 'media'], $media);
724
            }
725
726 View Code Duplication
            foreach ($alterations as $method => $arguments) {
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...
727
                if (is_array($arguments)) {
728
                    call_user_func_array([$image, $method], $arguments);
729
                } else {
730
                    call_user_func([$image, $method], $arguments);
731
                }
732
            }
733
734
            $alterations = $image;
735
        }
736
737
        $this->setSources($sources);
738
739
        return $this;
740
    }
741
742
    /**
743
     * Alter the image based on the user agents.
744
     *
745
     * @param  array $agents
746
     * @param  bool  $exit
747
     * @return $this
748
     */
749
    public function agents(array $agents, $exit = false)
750
    {
751
        foreach ($agents as $agent => $alterations) {
752
            if (
753
                $this->agent->is($agent)
754
                || ($agent == 'phone' && $this->agent->isPhone())
755
                || ($agent == 'mobile' && $this->agent->isMobile())
756
                || ($agent == 'tablet' && $this->agent->isTablet())
757
                || ($agent == 'desktop' && $this->agent->isDesktop())
758
            ) {
759 View Code Duplication
                foreach ($alterations as $method => $arguments) {
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...
760
                    if (is_array($arguments)) {
761
                        call_user_func_array([$this, $method], $arguments);
762
                    } else {
763
                        call_user_func([$this, $method], $arguments);
764
                    }
765
                }
766
767
                if ($exit) {
768
                    return $this;
769
                }
770
            }
771
        }
772
773
        return $this;
774
    }
775
776
    /**
777
     * Return if an extension is supported.
778
     *
779
     * @param $extension
780
     * @return bool
781
     */
782
    protected function supportsType($extension)
783
    {
784
        return !in_array($extension, ['svg', 'webp']);
785
    }
786
787
    /**
788
     * Set the image.
789
     *
790
     * @param  $image
791
     * @return $this
792
     */
793
    public function setImage($image)
794
    {
795
        if ($image instanceof Presenter) {
796
            $image = $image->getObject();
797
        }
798
799
        if ($image instanceof FieldType) {
800
            $image = $image->getValue();
801
        }
802
803
        // Replace path prefixes.
804
        if (is_string($image) && str_contains($image, '::')) {
805
            $image = $this->paths->realPath($image);
806
807
            $this->setExtension(pathinfo($image, PATHINFO_EXTENSION));
808
809
            $size = getimagesize($image);
810
811
            $this->setWidth(array_get($size, 0));
812
            $this->setHeight(array_get($size, 1));
813
        }
814
815
        if (is_string($image) && str_is('*://*', $image) && !starts_with($image, ['http', 'https'])) {
816
            $this->setExtension(pathinfo($image, PATHINFO_EXTENSION));
817
        }
818
819 View Code Duplication
        if ($image instanceof FileInterface) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
820
821
            /* @var FileInterface $image */
822
            $this->setExtension($image->getExtension());
823
824
            $this->setWidth($image->getWidth());
825
            $this->setHeight($image->getHeight());
826
        }
827
828 View Code Duplication
        if ($image instanceof FilePresenter) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\FilePresenter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
829
830
            /* @var FilePresenter|FileInterface $image */
831
            $image = $image->getObject();
832
833
            $this->setExtension($image->getExtension());
834
835
            $this->setWidth($image->getWidth());
836
            $this->setHeight($image->getHeight());
837
        }
838
839
        $this->image = $image;
840
841
        return $this;
842
    }
843
844
    /**
845
     * Make an image instance.
846
     *
847
     * @return \Intervention\Image\Image
848
     */
849
    protected function makeImage()
850
    {
851
        if ($this->image instanceof FileInterface) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
852
            return $this->manager->make(app(MountManager::class)->read($this->image->location()));
853
        }
854
855
        if (is_string($this->image) && str_is('*://*', $this->image)) {
856
            return $this->manager->make(app(MountManager::class)->read($this->image));
857
        }
858
859
        if ($this->image instanceof File) {
860
            return $this->manager->make($this->image->read());
861
        }
862
863
        if (is_string($this->image) && file_exists($this->image)) {
864
            return $this->manager->make($this->image);
865
        }
866
867
        if ($this->image instanceof Image) {
868
            return $this->image;
869
        }
870
871
        return null;
872
    }
873
874
    /**
875
     * Dump an image instance's data.
876
     *
877
     * @return string
878
     */
879
    protected function dumpImage()
880
    {
881
        if ($this->image instanceof FileInterface) {
0 ignored issues
show
Bug introduced by
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
882
            return app('League\Flysystem\MountManager')->read($this->image->location());
883
        }
884
885 View Code Duplication
        if (is_string($this->image) && str_is('*://*', $this->image) && !starts_with($this->image, ['http', '//'])) {
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...
886
            return app('League\Flysystem\MountManager')->read($this->image);
887
        }
888
889 View Code Duplication
        if (is_string($this->image) && (file_exists($this->image) || starts_with($this->image, ['http', '//']))) {
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...
890
            return file_get_contents($this->image);
891
        }
892
893
        if (is_string($this->image) && file_exists($this->image)) {
894
            return file_get_contents($this->image);
895
        }
896
897
        if ($this->image instanceof File) {
898
            return $this->image->read();
899
        }
900
901
        if ($this->image instanceof Image) {
902
            return $this->image->encode();
903
        }
904
905
        if (is_string($this->image) && file_exists($this->image)) {
906
            return file_get_contents($this->image);
907
        }
908
909
        return null;
910
    }
911
912
    /**
913
     * Get the image instance.
914
     *
915
     * @return \Intervention\Image\Image
916
     */
917
    public function getImage()
918
    {
919
        return $this->image;
920
    }
921
922
    /**
923
     * Get the file name.
924
     *
925
     * @return null|string
926
     */
927
    public function getFilename()
928
    {
929
        return $this->filename;
930
    }
931
932
    /**
933
     * Set the file name.
934
     *
935
     * @param $filename
936
     * @return $this
937
     */
938
    public function setFilename($filename = null)
939
    {
940
        $this->filename = $filename;
941
942
        return $this;
943
    }
944
945
    /**
946
     * Get the file name.
947
     *
948
     * @return null|string
949
     */
950
    public function getVersion()
951
    {
952
        return $this->version;
953
    }
954
955
    /**
956
     * Set the file name.
957
     *
958
     * @param $version
959
     * @return $this
960
     */
961
    public function setVersion($version = true)
962
    {
963
        $this->version = $version;
964
965
        return $this;
966
    }
967
968
    /**
969
     * Get the alterations.
970
     *
971
     * @return array
972
     */
973
    public function getAlterations()
974
    {
975
        return $this->alterations;
976
    }
977
978
    /**
979
     * Set the alterations.
980
     *
981
     * @param  array $alterations
982
     * @return $this
983
     */
984
    public function setAlterations(array $alterations)
985
    {
986
        $this->alterations = $alterations;
987
988
        return $this;
989
    }
990
991
    /**
992
     * Add an alteration.
993
     *
994
     * @param  $method
995
     * @param  $arguments
996
     * @return $this
997
     */
998
    public function addAlteration($method, $arguments = [])
999
    {
1000
        $this->alterations[$method] = $arguments;
1001
1002
        return $this;
1003
    }
1004
1005
    /**
1006
     * Get the attributes.
1007
     *
1008
     * @return array
1009
     */
1010
    public function getAttributes()
1011
    {
1012
        return $this->attributes;
1013
    }
1014
1015
    /**
1016
     * Set the attributes.
1017
     *
1018
     * @param  array $attributes
1019
     * @return $this
1020
     */
1021
    public function setAttributes(array $attributes)
1022
    {
1023
        $this->attributes = $attributes;
1024
1025
        return $this;
1026
    }
1027
1028
    /**
1029
     * Add an attribute.
1030
     *
1031
     * @param  $attribute
1032
     * @param  $value
1033
     * @return $this
1034
     */
1035
    protected function addAttribute($attribute, $value)
1036
    {
1037
        $this->attributes[$attribute] = $value;
1038
1039
        return $this;
1040
    }
1041
1042
    /**
1043
     * Get the srcsets.
1044
     *
1045
     * @return array
1046
     */
1047
    public function getSrcsets()
1048
    {
1049
        return $this->srcsets;
1050
    }
1051
1052
    /**
1053
     * Set the srcsets.
1054
     *
1055
     * @param  array $srcsets
1056
     * @return $this
1057
     */
1058
    public function setSrcsets(array $srcsets)
1059
    {
1060
        $this->srcsets = $srcsets;
1061
1062
        return $this;
1063
    }
1064
1065
    /**
1066
     * Get the sources.
1067
     *
1068
     * @return array
1069
     */
1070
    public function getSources()
1071
    {
1072
        return $this->sources;
1073
    }
1074
1075
    /**
1076
     * Set the sources.
1077
     *
1078
     * @param  array $sources
1079
     * @return $this
1080
     */
1081
    public function setSources(array $sources)
1082
    {
1083
        $this->sources = $sources;
1084
1085
        return $this;
1086
    }
1087
1088
    /**
1089
     * Get the quality.
1090
     *
1091
     * @param  null $default
1092
     * @return int
1093
     */
1094
    public function getQuality($default = null)
1095
    {
1096
        if (!$default) {
1097
            $this->config->get('streams::images.quality', 80);
1098
        }
1099
1100
        return $this->quality ?: $default;
1101
    }
1102
1103
    /**
1104
     * Set the output mode.
1105
     *
1106
     * @param $output
1107
     * @return $this
1108
     */
1109
    public function setOutput($output)
1110
    {
1111
        $this->output = $output;
1112
1113
        return $this;
1114
    }
1115
1116
    /**
1117
     * Get the extension.
1118
     *
1119
     * @return null|string
1120
     */
1121
    public function getExtension()
1122
    {
1123
        return $this->extension;
1124
    }
1125
1126
    /**
1127
     * Set the extension.
1128
     *
1129
     * @param $extension
1130
     * @return $this
1131
     */
1132
    public function setExtension($extension)
1133
    {
1134
        $this->extension = $extension;
1135
1136
        return $this;
1137
    }
1138
1139
    /**
1140
     * Get the allowed methods.
1141
     *
1142
     * @return array
1143
     */
1144
    public function getAllowedMethods()
1145
    {
1146
        return $this->allowedMethods;
1147
    }
1148
1149
    /**
1150
     * Add a path by it's namespace hint.
1151
     *
1152
     * @param $namespace
1153
     * @param $path
1154
     * @return $this
1155
     */
1156
    public function addPath($namespace, $path)
1157
    {
1158
        $this->paths->addPath($namespace, $path);
1159
1160
        return $this;
1161
    }
1162
1163
1164
    /**
1165
     * Get the width.
1166
     *
1167
     * @return int|null
1168
     */
1169
    public function getWidth()
1170
    {
1171
        return $this->width;
1172
    }
1173
1174
    /**
1175
     * Set the width.
1176
     *
1177
     * @param $width
1178
     * @return $this
1179
     */
1180
    public function setWidth($width)
1181
    {
1182
        $this->width = $width;
1183
1184
        return $this;
1185
    }
1186
1187
    /**
1188
     * Get the height.
1189
     *
1190
     * @return int|null
1191
     */
1192
    public function getHeight()
1193
    {
1194
        return $this->height;
1195
    }
1196
1197
    /**
1198
     * Set the height.
1199
     *
1200
     * @param $height
1201
     * @return $this
1202
     */
1203
    public function setHeight($height)
1204
    {
1205
        $this->height = $height;
1206
1207
        return $this;
1208
    }
1209
1210
    /**
1211
     * Guess the resize callback value
1212
     * from a boolean.
1213
     *
1214
     * @param array $arguments
1215
     */
1216
    protected function guessResizeArguments(array &$arguments)
1217
    {
1218
        $arguments = array_pad($arguments, 3, null);
1219
1220
        if (end($arguments) instanceof \Closure) {
1221
            return;
1222
        }
1223
1224
        if (array_pop($arguments) !== false && (is_null($arguments[0]) || is_null($arguments[1]))) {
1225
            $arguments[] = function (Constraint $constraint) {
1226
                $constraint->aspectRatio();
1227
            };
1228
        }
1229
    }
1230
1231
    /**
1232
     * Return the output.
1233
     *
1234
     * @return string
1235
     */
1236
    public function __toString()
1237
    {
1238
        return (string)$this->output();
1239
    }
1240
1241
    /**
1242
     * If the method does not exist then
1243
     * add an attribute and return.
1244
     *
1245
     * @param $name
1246
     * @param $arguments
1247
     * @return $this|mixed
1248
     */
1249
    public function __call($name, $arguments)
1250
    {
1251
        if (in_array($name, $this->getAllowedMethods())) {
1252
            return $this->addAlteration($name, $arguments);
1253
        }
1254
1255
        if ($this->macros->isMacro($macro = snake_case($name))) {
1256
            return $this->macro($macro);
1257
        }
1258
1259
        if (!method_exists($this, $name)) {
1260
            array_set($this->attributes, $name, array_shift($arguments));
1261
1262
            return $this;
1263
        }
1264
1265
        return call_user_func_array([$this, $name], $arguments);
1266
    }
1267
}
1268