PrintTicketsCommand::execute()   F
last analyzed

Complexity

Conditions 12
Paths 514

Size

Total Lines 69
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 69
rs 3.5364
cc 12
eloc 51
nc 514
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Command;
9
10
use BCRM\BackendBundle\Entity\Event\Event;
11
use BCRM\BackendBundle\Entity\Event\Registration;
12
use BCRM\BackendBundle\Entity\Event\Ticket;
13
use BCRM\BackendBundle\Exception\CommandException;
14
use BCRM\BackendBundle\Service\Event\CreateTicketCommand;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class PrintTicketsCommand extends ContainerAwareCommand
22
{
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('bcrm:tickets:print')
27
            ->setDescription('Print tickets')
28
            ->addOption('url', 'u', InputOption::VALUE_REQUIRED, 'URL to the printing queue.')
29
            ->addOption('template', 't', InputOption::VALUE_REQUIRED, 'Template file for the badges.')
30
            ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output directory for the badges.');
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $url      = $input->getOption('url');
36
        $username = parse_url($url, PHP_URL_USER);
37
        $password = parse_url($url, PHP_URL_PASS);
38
39
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
40
            $output->writeln(sprintf('Connecting to %s …', $url));
41
        }
42
43
        $env      = new \Twig_Environment(new \Twig_Loader_String());
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_String has been deprecated with message: since 1.18.1 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44
        $template = file_get_contents($input->getOption('template'));
45
46
        // Fetch list of tickets which need to be printed
47
        $queue = json_decode(file_get_contents($url));
48
        foreach ($queue->items as $ticket) {
49
            $ticketUrl = $ticket->{'@subject'};
50
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
51
                $output->writeln(sprintf('Printing %s …', $ticketUrl));
52
            }
53
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
54
                $output->writeln(sprintf('%s (%s)', $ticket->code, $ticket->name));
55
            }
56
            // Create bade
57
            $data            = $ticket;
58
            $nameParts       = explode(' ', $ticket->name, 2);
59
            $tags            = explode(' ', $ticket->tags);
60
            $data->tag1      = isset($tags[0]) ? $tags[0] : null;
61
            $data->tag2      = isset($tags[1]) ? $tags[1] : null;
62
            $data->tag3      = isset($tags[2]) ? $tags[2] : null;
63
            $data->firstname = $nameParts[0];
64
            $data->lastname  = isset($nameParts[1]) ? $nameParts[1] : null;
65
            $data->day       = $ticket->day == Ticket::DAY_SATURDAY ? 'Sa' : 'So';
66
            $badge           = $env->render($template, (array)$data);
67
            $badgeFileName   = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $ticket->code;
68
            $badgeSVG        = $badgeFileName . '.svg';
69
            $badgePDF        = $badgeFileName . '.pdf';
70
            file_put_contents($badgeSVG, $badge);
71
            exec(
72
                sprintf(
73
                    '`which inkscape` --export-pdf=%s %s',
74
                    escapeshellarg($badgePDF),
75
                    escapeshellarg($badgeSVG)
76
                )
77
            );
78
            $printFile = rtrim($input->getOption('output'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $ticket->code . '.pdf';
79
            if (is_file($printFile)) {
80
                $counter = 1;
81
                do {
82
                    $printFile = preg_replace('/(\.[0-9]+)*\.pdf$/', '.' . ($counter++) . '.pdf', $printFile);
83
                } while (is_file($printFile));
84
            }
85
            copy($badgePDF, $printFile);
86
            unlink($badgePDF);
87
            unlink($badgeSVG);
88
            // Mark ticket as printed
89
            $context = array(
90
                'http' => array(
91
                    'method' => "PATCH",
92
                    'header' => sprintf(
93
                        'Authorization: Basic %s',
94
                        base64_encode(
95
                            sprintf('%s:%s', $username, $password)
96
                        )),
97
                )
98
            );
99
            file_get_contents($ticketUrl, null, stream_context_create($context));
100
        }
101
    }
102
}
103