Passed
Push — master ( 531d72...2af71e )
by Simon
02:00
created

Typo3Mode::checkRequestType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Gilbertsoft\Lib\Utility;
3
4
/*
5
 * This file is part of the "GS Library" Extension for TYPO3 CMS.
6
 *
7
 * Copyright (C) 2017 by Gilbertsoft (gilbertsoft.org)
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * For the full license information, please read the LICENSE file that
20
 * was distributed with this source code.
21
 *
22
 * The TYPO3 project - inspiring people to share!
23
 */
24
25
/**
26
 * Typo3Mode class.
27
 *
28
 * USE:
29
 * The class is intended to be used without creating an instance of it.
30
 * So: Do not instantiate - call functions with "\Gilbertsoft\TYPO3\Warranty\Utility\Provider::" prefixed the function name.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
31
 * So use \Gilbertsoft\TYPO3\Warranty\Utility\Provider::[method-name] to refer to the functions, eg. '\Gilbertsoft\TYPO3\Warranty\Utility\Provider::getName()'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 158 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
32
 */
33
class Typo3Mode
34
{
35
    /**
36
     * Returns TRUE if mode is set.
37
     *
38
     * @return bool
39
     * @api
40
     */
41
    public static function checkMode()
42
    {
43
        return defined('TYPO3_MODE');
44
    }
45
46
    /**
47
     * Returns TRUE if request type is set.
48
     *
49
     * @return bool
50
     * @api
51
     */
52
    public static function checkRequestType()
53
    {
54
        return defined('TYPO3_REQUESTTYPE');
55
    }
56
57
    /**
58
     * Returns TRUE if called in frontend mode.
59
     *
60
     * @return bool
61
     * @api
62
     */
63
    public static function isFrontend()
64
    {
65
        return (self::checkMode() && (TYPO3_MODE == 'FE')) || (self::checkRequestType() && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
66
    }
67
68
    /**
69
     * Returns TRUE if called in backend mode.
70
     *
71
     * @return bool
72
     * @api
73
     */
74
    public static function isBackend()
75
    {
76
        return (self::checkMode() && (TYPO3_MODE == 'BE')) || (self::checkRequestType() && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
77
    }
78
79
    /**
80
     * Returns TRUE if called in CLI mode.
81
     *
82
     * @return bool
83
     * @api
84
     */
85
    public static function isCli()
86
    {
87
        return (defined('TYPO3_cliMode') && TYPO3_cliMode === true) || (self::checkRequestType() && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
88
    }
89
90
    /**
91
     * Returns TRUE if called in ajax mode.
92
     *
93
     * @return bool
94
     * @api
95
     */
96
    public static function isAjax()
0 ignored issues
show
Coding Style introduced by
isAjax uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
97
    {
98
        return ($GLOBALS['TYPO3_AJAX']) || (self::checkRequestType() && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX));
99
    }
100
101
    /**
102
     * Returns TRUE if called in install mode.
103
     *
104
     * @return bool
105
     * @api
106
     */
107
    public static function isInstall()
108
    {
109
        return (defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript) || (self::checkRequestType() && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_INSTALL));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 162 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
    }
111
112
    /**
113
     * Returns TRUE if called in composer mode.
114
     *
115
     * @return bool
116
     * @api
117
     */
118
    public static function isComposerMode()
119
    {
120
        return (defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE);
121
    }
122
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
123