Completed
Push — master ( 7c218c...593722 )
by Jake
02:19
created

SEOGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.4285
1
<?php
2
3
namespace MeestorHok\Blue;
4
5
use Artesaos\SEOTools\Facades\SEOTools as SEOTools;
6
use Request;
7
use File;
8
9
class SEOGenerator
10
{
11
    public $defaults;
12
    public static $favicons;
13
    
14
    public function __construct () {
15
        $this->defaults = [
16
            'title' => 'Home',
17
            'title-spacer' => ' &#8211; ',
18
            'title-description' => 'Laravel Blue',
19
            'keywords' => array_collapse([['home'], ['laravel', 'blue', 'cms', 'meestorhok']]),
20
            'description' => 'This amazing site was created by Laravel Blue!',
21
            'images' => [],
22
            'twitter' => '@BlueCMS',
23
            'copyright' => '© 2016 Laravel Blue',
24
            'robots' => 'index,follow',
25
            'icon_dir' => '/blue/img/icons',
26
            'colors' => [
27
                'safari_pinned' => '#5bbad5',
28
                'ms_tile' => '#da532c',
29
                'theme' => '#ec500e'
30
            ]
31
        ];
32
    }
33
    
34
    public function generateSEO (array $page)
35
    {
36
        SEOTools::metatags()
37
            ->setTitleDefault($page['title-description'])
38
            ->setTitleSeparator($page['title-spacer'])
39
            ->setTitle($page['title'])
40
            ->setDescription($page['description'])
41
            ->setCanonical(Request::url())
42
            ->addMeta('copyright', $page['copyright'])
43
            ->addMeta('robots', $page['robots'])
44
            ->addMeta('HandheldFriendly', 'True')
45
            ->addMeta('MobileOptimized', '320')
46
            ->addMeta('X-UA-Compatible', 'IE=edge', 'http-equiv')
47
            ->addMeta('viewport', 'width=device-width, initial-scale=1, shrink-to-fit=no');
48
        SEOTools::opengraph()
49
            ->setTitle($page['title'].$page['title-spacer'].$page['title-description'])
50
            ->setDescription($page['description'])
51
            ->setUrl(Request::url())
52
            ->addProperty('locale', 'en_US')
53
            ->addProperty('type', 'website');
54
        SEOTools::twitter()
55
            ->setSite($page['twitter'])
56
            ->setTitle($page['title'].$page['title-spacer'].$page['title-description'])
57
            ->setDescription($page['description'])
58
            ->setType('summary')
59
            ->setUrl(Request::url());
60
        foreach ($page['images'] as $image) {
61
            SEOTools::opengraph()->addImage(asset($image['uri']));
62
            SEOTools::twitter()->addImage(asset($image['uri']));
63
        }
64
        foreach ($page['keywords'] as $keyword) {
65
            SEOTools::metatags()->addKeyword($keyword);
66
        }
67
    }
68
    
69
    public function setFavicons () {
70
        $favicon_dir = insert_if_exists($this->defaults['icon_dir']);
71
        $colors = $this->defaults['colors'];
72 View Code Duplication
        return  insert_if_exists (['60x60', '72x72', '114x114', '120x120', '152x152', '180x180'], function ($insert) use ($favicon_dir) {
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...
73
                        return '<link rel="apple-touch-icon" sizes="' . $insert . '" href="' . asset($favicon_dir . '/apple-touch-icon-' . $insert . '.png').'">';
74
                    }, function ($insert) use ($favicon_dir) {
75
                        return File::exists(public_path($favicon_dir.'/apple-touch-icon-'.$insert.'.png'));
76
                    }) . 
77 View Code Duplication
                insert_if_exists (['16x16', '32x32', '96x96', '194x194'], function ($insert) use ($favicon_dir) {
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...
78
                        return '<link rel="icon" type="image/png" href="' . asset($favicon_dir . '/favicon-' . $insert . '.png').'" sizes="'.$insert.'">';
79
                    }, function ($insert) use ($favicon_dir) {
80
                        return File::exists(public_path($favicon_dir.'/favicon-'.$insert.'.png'));
81
                    }) . 
82 View Code Duplication
                insert_if_exists (['192x192'], function ($insert) use ($favicon_dir) {
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...
83
                        return '<link rel="icon" type="image/png" href="' . asset($favicon_dir . '/android-chrome-' . $insert . '.png').'" sizes="'.$insert.'">';
84
                    }, function ($insert) use ($favicon_dir) {
85
                        return File::exists(public_path($favicon_dir.'/android-chrome-'.$insert.'.png'));
86
                    }) . 
87
                insert_if_exists ($favicon_dir.'/manifest.json', function ($insert) {
88
                        return '<link rel="manifest" href="' . asset($insert) . '">';
89
                    }, function ($insert) {
90
                        return File::exists(public_path($insert));
91
                    }) . 
92
                insert_if_exists ($favicon_dir.'/safari-pinned-tab.svg', function ($insert) use ($colors) {
93
                        return '<link rel="mask-icon" href="'.asset($insert).'" color="' . insert_if_exists($colors['safari_pinned']) . '">';
94
                    }, function ($insert) {
95
                        return File::exists(public_path($insert));
96
                    }) . 
97
                insert_if_exists ($favicon_dir.'/favicon.ico', function ($insert) {
98
                        return '<link rel="shortcut icon" href="'.asset($insert).'">';
99
                    }, function ($insert) {
100
                        return File::exists(public_path($insert));
101
                    }) . 
102
                insert_if_exists ($favicon_dir.'/mstile-144x144.png', function ($insert) {
103
                        return '<meta name="msapplication-TileImage" content="'.asset($insert).'">';
104
                    }, function ($insert) {
105
                        return File::exists(public_path($insert));
106
                    }) . 
107
                insert_if_exists ($favicon_dir.'/browserconfig.xml', function ($insert) {
108
                        return '<meta name="msapplication-config" content="'.asset($insert).'">';
109
                    }, function ($insert) {
110
                        return File::exists(public_path($insert));
111
                    }) . 
112
                insert_if_exists ($colors['ms_tile'], function ($insert) {
113
                        return '<meta name="msapplication-TileColor" content="'.$insert.'">';
114
                    }) . 
115
                insert_if_exists ($colors['theme'], function ($insert) {
116
                        return '<meta name="theme-color" content="'.$insert.'">';
117
                    });
118
    }
119
    
120
    public function make (array $details, $view = null)
121
    {
122
        $this->defaults = array_replace($this->defaults, $details);
123
        
124
        $this->generateSEO($this->defaults);
125
        
126
        if (!is_null($view)) {
127
            return view($view);
128
        }
129
    }
130
    
131
    public function get ()
132
    {
133
        return str_replace(PHP_EOL, '', SEOTools::generate()) . $this->setFavicons();
134
    }
135
}