Issues (18)

src/Tasks/CheckAllTemplates.php (2 issues)

1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Environment;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\ORM\ArrayList;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\View\Requirements;
13
use SilverStripe\View\SSViewer;
14
use SilverStripe\View\ViewableData;
15
use Sunnysideup\TemplateOverview\Api\AllLinks;
16
17
class CheckAllTemplates extends BuildTask
18
{
19
    protected $title = 'Check URLs for HTTP errors';
20
21
    protected $description = 'Will go through main URLs (all page types (e.g Page, MyPageTemplate), all page types in CMS (e.g. edit Page, edit HomePage, new MyPage) and all models being edited in ModelAdmin, checking for HTTP response errors (e.g. 404). Click start to run.';
22
23
    private static $segment = 'smoketest';
24
25
    /**
26
     * Main function
27
     * has two streams:
28
     * 1. check on url specified in GET variable.
29
     * 2. create a list of urls to check.
30
     *
31
     * @param \SilverStripe\Control\HTTPRequest $request
32
     */
33
    public function run($request)
34
    {
35
        ini_set('max_execution_time', 3000);
36
37
        //we have this check here so that even in dev mode you have to log in.
38
        //because if you do not log in, the test will not work.
39
        if (! Permission::check('ADMIN')) {
40
            die('Please <a href="/Security/login/?BackURL=/dev/tasks/smoketest/">log in</a> first.');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
41
42
            return;
0 ignored issues
show
return is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
43
        }
44
45
        $allLinks = Injector::inst()->get(AllLinks::class)->getAllLinks();
46
        if (! empty($_GET['htmllist'])) {
47
            $this->htmlListOutput($allLinks);
48
49
            return;
50
        }
51
52
        $this->defaultOutput($allLinks);
53
    }
54
55
    protected function defaultOutput(array $allLinks)
56
    {
57
        $count = 0;
58
        $sections = ['allNonCMSLinks', 'allCMSLinks'];
59
        $links = ArrayList::create();
60
61
        foreach ($sections as $isCMSLink => $sectionVariable) {
62
            foreach ($allLinks[$sectionVariable] as $link) {
63
                ++$count;
64
65
                $links->push(ArrayData::create([
66
                    'IsCMSLink' => $isCMSLink,
67
                    'Link' => $link,
68
                    'ItemCount' => $count,
69
                ]));
70
            }
71
        }
72
73
        $otherLinks = '';
74
        $className = '';
75
        foreach ($allLinks['otherLinks'] as $linkArray) {
76
            if ($linkArray['ClassName'] !== $className) {
77
                $className = $linkArray['ClassName'];
78
                $otherLinks .= '</ul><h2>' . $className . '</h2><ul>';
79
            }
80
81
            $otherLinks .= '<li><a href="' . $linkArray['Link'] . '">' . $linkArray['Link'] . '</a></li>';
82
        }
83
84
        Requirements::javascript('https://code.jquery.com/jquery-3.3.1.min.js');
85
        Requirements::javascript('sunnysideup/templateoverview:client/javascript/checkalltemplates.js');
86
        Requirements::themedCSS('client/css/checkalltemplates');
87
88
        $template = new SSViewer('CheckAllTemplates');
89
90
        echo $template->process(
91
            ViewableData::create(),
92
            [
93
                'Title' => $this->title,
94
                'Links' => $links,
95
                'OtherLinks' => $otherLinks,
96
                'AbsoluteBaseURLMinusSlash' => $this->baseURL(),
97
                'HasEnvironmentVariable' => ((bool) Environment::getEnv('SS_ALLOW_SMOKE_TEST')),
98
            ]
99
        );
100
    }
101
102
    protected function baseURL()
103
    {
104
        return trim(Director::absoluteBaseURL(), '/');
105
    }
106
107
    protected function htmlListOutput(array $allLinks)
108
    {
109
        $base = $this->baseURL();
110
        $array = [];
111
        foreach ($allLinks as $key => $list) {
112
            foreach ($list as $item) {
113
                if ('otherLinks' === $key) {
114
                    $link = $base . $item['Link'];
115
                    $title = $base . $item['Link'];
116
                } else {
117
                    $link = $base . $item;
118
                    $title = $base . $item;
119
                }
120
121
                $array[$link] = '<a href="' . $link . '">' . $title . '</a>';
122
            }
123
        }
124
125
        if ([] !== $array) {
126
            ksort($array);
127
            echo '<ol><li>' . implode('</li><li>', $array) . '</li></ol>';
128
        } else {
129
            echo 'No links available';
130
        }
131
    }
132
}
133