PrintingController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 9
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B queueAction() 0 33 3
A printAction() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @author    Markus Tacker <[email protected]>
5
 * @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
6
 */
7
8
namespace BCRM\PrintBundle\Controller;
9
10
use BCRM\BackendBundle\Entity\Event\Event;
11
use BCRM\BackendBundle\Entity\Event\EventRepository;
12
use BCRM\BackendBundle\Entity\Event\RegistrationRepository;
13
use BCRM\BackendBundle\Entity\Event\Ticket;
14
use BCRM\BackendBundle\Entity\Event\TicketRepository;
15
use BCRM\PrintBundle\Exception\AccesDeniedHttpException;
16
use BCRM\PrintBundle\Exception\NotFoundHttpException;
17
use Carbon\Carbon;
18
use LiteCQRS\Bus\CommandBus;
19
use LiteCQRS\Plugin\CRUD\Model\Commands\UpdateResourceCommand;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\RouterInterface;
23
24
/**
25
 * Dashboard for the event concierge.
26
 */
27
class PrintingController
28
{
29 View Code Duplication
    public function __construct(EventRepository $eventRepo, RegistrationRepository $registrationRepo, TicketRepository $ticketRepo, CommandBus $commandBus, RouterInterface $router, $schemeAndHost)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        $this->eventRepo        = $eventRepo;
0 ignored issues
show
Bug introduced by
The property eventRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->ticketRepo       = $ticketRepo;
0 ignored issues
show
Bug introduced by
The property ticketRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->registrationRepo = $registrationRepo;
0 ignored issues
show
Bug introduced by
The property registrationRepo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
        $this->commandBus       = $commandBus;
0 ignored issues
show
Bug introduced by
The property commandBus does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        $this->router           = $router;
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->schemeAndHost    = trim($schemeAndHost, '/ ');
0 ignored issues
show
Bug introduced by
The property schemeAndHost does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    /**
40
     * List unprinted tickets.
41
     */
42
    public function queueAction()
43
    {
44
        /* @var $event Event */
45
        $event = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.'));
46
47
        // Do not allow checkins on the wrong day
48
        $now   = new Carbon();
49
        $start = Carbon::createFromTimestamp($event->getStart()->getTimestamp());
50
        $day   =
0 ignored issues
show
Unused Code introduced by
$day is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
            $start->setTime(0, 0, 0);
52
        $end   = clone $start;
53
        $end->setTime(23, 59, 59);
54
        $day   = $now->between($start, $end) ? Ticket::DAY_SATURDAY : Ticket::DAY_SUNDAY;
55
        $items = array();
56
        foreach ($this->ticketRepo->getUnprintedTickets($event, $day) as $ticket) {
57
            $registration = $this->registrationRepo->getRegistrationForEmail($event, $ticket->getEmail())->get();
58
            $type         = null;
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
            $items[]      = array(
60
                '@context' => 'http://barcamp-rheinmain.de/jsonld/Ticket',
61
                '@subject' => $this->schemeAndHost . $this->router->generate('bcrmprint_ticket', array('id' => $ticket->getId(), 'code' => $ticket->getCode())),
62
                'name'     => $ticket->getName(),
63
                'twitter'  => $registration->getTwitter(),
64
                'code'     => $ticket->getCode(),
65
                'day'      => $ticket->getDay(),
66
                'tags'     => $registration->getTags(),
67
            );
68
        }
69
        $data     = array('items' => $items);
70
        $response = new Response(json_encode($data));
71
        $response->setCharset('utf-8');
72
        $response->headers->set('Content-Type', 'application/json');
73
        return $response;
74
    }
75
76
    /**
77
     * Mark the given ticket as printed.
78
     *
79
     * @param Request $request
80
     * @param int     $id
81
     * @param string  $code
82
     *
83
     * @return Response
84
     */
85
    public function printAction(Request $request, $id, $code)
86
    {
87
        $ticket               = $this->ticketRepo->getTicketByIdAndCode($id, $code)->getOrThrow(new NotFoundHttpException('Ticket not found.'));
0 ignored issues
show
Unused Code introduced by
$ticket is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
        $updateCommand        = new UpdateResourceCommand();
89
        $updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket';
90
        $updateCommand->id    = $id;
91
        $updateCommand->data  = array('printed' => $request->getMethod() === 'PATCH');
92
        $this->commandBus->handle($updateCommand);
93
        $response = new Response(json_encode(array('status' => 'OK')));
94
        $response->setCharset('utf-8');
95
        $response->headers->set('Content-Type', 'application/json');
96
        return $response;
97
    }
98
}
99