Completed
Push — next ( 8ef386...43bb99 )
by Thomas
11:01 queued 05:16
created

QrCodeController::__construct()   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
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Oc\GeoCache\Controller;
4
5
use Endroid\QrCode\ErrorCorrectionLevel;
6
use Endroid\QrCode\LabelAlignment;
7
use Endroid\QrCode\QrCode;
8
use Oc\GeoCache\Persistence\GeoCache\GeoCacheEntity;
9
use Oc\GeoCache\Persistence\GeoCache\GeoCacheService;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class QrCodeController extends Controller
16
{
17
    /**
18
     * @var GeoCacheService
19
     */
20
    private $geoCacheService;
21
22
    public function __construct(GeoCacheService $geoCacheService)
23
    {
24
        $this->geoCacheService = $geoCacheService;
25
    }
26
27
    /**
28
     * @param Request $request
29
     * @return Response
30
     * @Route("/api/geocache/qrCodes")
31
     */
32
    public function generateQrCode(Request $request)
33
    {
34
        $geoCache = $this->geoCacheService->fetchByWaypoint($request->get('wp'));
35
36
        if (!$geoCache instanceof GeoCacheEntity) {
37
            throw new \InvalidArgumentException('the waypoint is not valid!');
38
        }
39
40
        $qrCode = new QrCode('https://www.opencaching.de/' . $geoCache->wpOc);
41
        $qrCode->setSize(300);
42
43
        $qrCode
44
            ->setWriterByName('png')
45
            ->setMargin(10)
46
            ->setEncoding('UTF-8')
47
            ->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)
48
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0])
49
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255])
50
            ->setLabel('www.opencaching.de', 16, null, LabelAlignment::CENTER)
51
            ->setLogoPath(__DIR__ . '/../../../../theme/img/logo/oc-logo.png')
52
            ->setLogoWidth(250)
53
            ->setValidateResult(false);
54
55
        return new Response(
56
            $qrCode->writeString(),
57
            Response::HTTP_OK,
58
            ['Content-Type' => $qrCode->getContentType()]
59
        );
60
    }
61
}
62