Passed
Branch master (b21e04)
by Xavier
04:05 queued 02:00
created

AppController::redirectHome()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\Controller;
11
12
use Slim\Http\Request;
13
use Slim\Http\Response;
14
Use App\Model\User;
15
16
class AppController extends Controller
17
{
18
19
    /**
20
     * Redirects the user to the home page with the appropriated language following the parameters given
21
     *
22
     * @param Request $request
23
     * @param Response $response
24
     * @return Response
25
     */
26
    public function redirectHome(Request $request, Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

26
    public function redirectHome(/** @scrutinizer ignore-unused */ Request $request, Response $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
redirectHome uses the super-global variable $_SERVER 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...
Coding Style introduced by
redirectHome uses the super-global variable $_COOKIE 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...
27
    {
28
29
        $clientLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
30
31
        if (isset($_COOKIE['country']) && $this->checkCountry($_COOKIE['country'])) { // First if the client has a preference
32
            $country_id = $_COOKIE['country'];
33
        }else if ($this->checkCountry($clientLanguage)) { // Not a preference? Let's check its browser!
34
            $country_id = $clientLanguage;
35
        } else {  // Nevermind let's move to the french language
36
            $country_id = 'fr';
37
        }
38
39
        return $response->withRedirect($this->router->pathFor('home', ['country' => $country_id]));
40
    }
41
42
    /**
43
     * Renders the home page
44
     *
45
     * @param Request $request
46
     * @param Response $response
47
     * @return \Psr\Http\Message\ResponseInterface
48
     */
49
    public function home(Request $request, Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function home(/** @scrutinizer ignore-unused */ Request $request, Response $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        return $this->view->render($response, 'App/home.twig');
52
    }
53
54
55
    /**
56
     * Checks for a country id given if a dictionary is associated.
57
     *
58
     * @param string $country_id
59
     * @return bool
60
     */
61
    private function checkCountry($country_id)
62
    {
63
        return file_exists(dirname(__FILE__) . '/../Resources/' . DICTIONARY_PATH . $country_id . '.json');
0 ignored issues
show
Bug introduced by
The constant App\Controller\DICTIONARY_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
    }
65
}
66