Completed
Pull Request — master (#18)
by Valentyn
02:52
created

ApiTokenRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 47.83%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
ccs 11
cts 23
cp 0.4783
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findByToken() 0 7 1
A findOneByToken() 0 15 3
A findOneByIdJoinedToCategory() 0 15 2
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Repository;
5
6
use App\Entity\ApiToken;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * @method ApiToken|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method ApiToken|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method ApiToken[]    findAll()
14
 * @method ApiToken[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class ApiTokenRepository extends ServiceEntityRepository
17
{
18 7
    public function __construct(RegistryInterface $registry)
19
    {
20 7
        parent::__construct($registry, ApiToken::class);
21 7
    }
22
23
    //todo user doesn't loaded
24 1
    public function findByToken(string $token): ?ApiToken
25
    {
26 1
        return $this->findOneByToken($token);
27
        /*return $this->findOneBy([
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
            'token' => $token,
29
        ]);*/
30
    }
31
32 1
    public function findOneByToken(string $token): ?ApiToken
33
    {
34 1
        $query = $this->getEntityManager()
35 1
            ->createQuery(
36 1
                'SELECT t, u FROM App\Entity\ApiToken t JOIN t.user u WHERE t.token = :token'
37 1
            )->setParameter('token', $token);
38
39
        try {
40 1
            return $query->getSingleResult();
41
        } catch (\Doctrine\ORM\NoResultException $e) {
42
            return null;
43
        } catch (\Doctrine\ORM\NonUniqueResultException $e) {
44
            return null;
45
        }
46
    }
47
48
    public function findOneByIdJoinedToCategory($productId)
49
    {
50
        $query = $this->getEntityManager()
51
            ->createQuery(
52
                'SELECT p, c FROM AppBundle:Product p
53
        JOIN p.category c
54
        WHERE p.id = :id'
55
            )->setParameter('id', $productId);
56
57
        try {
58
            return $query->getSingleResult();
59
        } catch (\Doctrine\ORM\NoResultException $e) {
60
            return null;
61
        }
62
    }
63
}
64