EnvironmentGenerator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 34
c 2
b 1
f 0
dl 0
loc 86
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getFont() 0 7 2
A createEnvironmentTextImage() 0 9 1
A calculateDynamicFontSize() 0 10 3
A shouldGenerateFavicon() 0 3 1
A generate() 0 26 1
1
<?php
2
3
namespace BeyondCode\LaravelFavicon\Generators;
4
5
use BeyondCode\LaravelFavicon\Favicon;
6
use Illuminate\Http\Response;
7
use Intervention\Image\AbstractFont;
8
use Intervention\Image\Gd\Font as GdFont;
9
use Intervention\Image\Image;
10
use Intervention\Image\ImageManager;
11
use Intervention\Image\Imagick\Font as ImagickFont;
12
13
class EnvironmentGenerator implements FaviconGenerator
14
{
15
    /** @var Favicon */
16
    protected $favicon;
17
18
    /** @var ImageManager */
19
    protected $manager;
20
21
    /** @var string */
22
    protected $environment;
23
24
    public function __construct(Favicon $favicon)
25
    {
26
        $this->favicon = $favicon;
27
28
        $this->manager = new ImageManager([
29
            'driver' => config('favicon.image_driver'),
30
        ]);
31
32
        $this->environment = config('app.env');
33
    }
34
35
    public function generate(string $icon): Response
36
    {
37
        $paddingX = config('favicon.padding.x');
38
        $paddingY = config('favicon.padding.y');
39
40
        $img = $this->manager->make($icon);
41
42
        $font = $this->getFont($this->favicon->getFaviconText($this->environment));
0 ignored issues
show
Bug introduced by
It seems like $this->favicon->getFavic...ext($this->environment) can also be of type null; however, parameter $text of BeyondCode\LaravelFavico...entGenerator::getFont() does only seem to accept string, 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

42
        $font = $this->getFont(/** @scrutinizer ignore-type */ $this->favicon->getFaviconText($this->environment));
Loading history...
43
44
        $font->file(config('favicon.font') ?? __DIR__.'/../../resources/fonts/OpenSans-Regular.ttf');
45
46
        $font->valign('top');
47
48
        $font->color($this->favicon->getFaviconColor($this->environment));
49
50
        $this->calculateDynamicFontSize($font, $img, $paddingX);
51
52
        $environmentTextImage = $this->createEnvironmentTextImage($font);
53
54
        $output = $this->manager->canvas($img->width(), $img->height());
55
56
        $output->insert($img);
57
58
        $output->insert($environmentTextImage, 'bottom-right', $paddingX, $paddingY);
59
60
        return $output->response('png');
61
    }
62
63
    protected function getFont(string $text): AbstractFont
64
    {
65
        if (config('favicon.image_driver') === 'imagick') {
66
            return new ImagickFont($text);
67
        }
68
69
        return new GdFont($text);
70
    }
71
72
    protected function calculateDynamicFontSize(AbstractFont $font, Image $icon, $paddingX)
73
    {
74
        $size = $font->getBoxSize();
75
76
        while ($size['width'] + $paddingX > $icon->width() && $font->getSize() > 0) {
77
            $fontSize = $font->getSize();
78
79
            $font->size($fontSize - 1);
80
81
            $size = $font->getBoxSize();
82
        }
83
    }
84
85
    protected function createEnvironmentTextImage(AbstractFont $font)
86
    {
87
        $size = $font->getBoxSize();
88
89
        $environmentText = $this->manager->canvas($size['width'], $size['height'], $this->favicon->getFaviconBackgroundColor($this->environment));
90
91
        $font->applyToImage($environmentText);
92
93
        return $environmentText;
94
    }
95
96
    public function shouldGenerateFavicon(): bool
97
    {
98
        return config('favicon.enabled_environments.'.$this->environment) !== null;
99
    }
100
}
101