Completed
Push — development ( 689729...8ef386 )
by Thomas
18s
created

QrCodeController::generateQrCode()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 1
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
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(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]
57
        );
58
    }
59
}
60