Completed
Pull Request — master (#137)
by
unknown
12:08
created

CustomerController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 71
rs 10
c 2
b 0
f 1
wmc 6
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newAction() 0 18 3
A updateAction() 0 17 2
A logoutAction() 0 16 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Customer;
6
use AppBundle\Model\CustomerResponse;
7
use FOS\RestBundle\Controller\Annotations\RouteResource;
8
use FOS\RestBundle\View\View;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * @RouteResource("Customer")
14
 */
15
class CustomerController extends Controller
16
{
17
    /**
18
     * @param Request $request
19
     *
20
     * @return CustomerResponse
21
     */
22
    public function newAction(Request $request)
23
    {
24
        $apiKey = $request->headers->get('API-Key-Token');
25
        $user = $this->getUser();
26
27
        if (!$user && !$apiKey) {
28
            $customer = $this->get('customer_login')
29
                ->newCustomer();
30
31
            return new CustomerResponse($customer);
32
        }
33
34
        $response = [
35
            '401' => 'Invalid API-Key-Token',
36
        ];
37
38
        return View::create($response, 401);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FOS\RestBundle\V...create($response, 401); (FOS\RestBundle\View\View) is incompatible with the return type documented by AppBundle\Controller\CustomerController::newAction of type AppBundle\Model\CustomerResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
39
    }
40
41
    /**
42
     * @param Request $request
43
     *
44
     * @return CustomerResponse
45
     */
46
    public function updateAction(Request $request)
47
    {
48
        $customer = $this->get('customer_login')
49
            ->updateCustomer(
50
                $request->headers->get('API-Key-Token'),
51
                $request->getContent()
52
            );
53
        if ($customer) {
54
            return new CustomerResponse($customer);
55
        }
56
57
        $response = [
58
            '401' => 'invalid first_name, last_name, email',
59
        ];
60
61
        return View::create($response, 401);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FOS\RestBundle\V...create($response, 401); (FOS\RestBundle\View\View) is incompatible with the return type documented by AppBundle\Controller\Cus...ontroller::updateAction of type AppBundle\Model\CustomerResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
62
    }
63
64
    /**
65
     * @param Request $request
66
     *
67
     * @return View
68
     */
69
    public function logoutAction(Request $request)
70
    {
71
        $apiKeyHead = $request->headers->get('API-Key-Token');
72
73
        $response = [
74
            '204' => 'Successful operation',
75
        ];
76
77
        $em = $this->getDoctrine()->getManager();
78
        $customer = $em->getRepository('AppBundle:Customer')
79
            ->findOneBy(['apiKey' => $apiKeyHead]);
80
        $customer->setApiKey(null);
81
        $em->flush();
82
83
        return View::create($response, 204);
84
    }
85
}
86