Passed
Branch master (1fd71c)
by Nicolas
03:28
created

UnfollowProfileController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Profile;
4
5
use App\Entity\User;
6
use App\Security\UserResolver;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
/**
13
 * @Route("/api/profiles/{username}/follow", name="api_profiles_unfollow")
14
 * @Method("DELETE")
15
 *
16
 * @Security("is_granted('ROLE_USER')")
17
 */
18
final class UnfollowProfileController
19
{
20
    /**
21
     * @var EntityManagerInterface
22
     */
23
    private $entityManager;
24
25
    /**
26
     * @var UserResolver
27
     */
28
    private $userResolver;
29
30
    /**
31
     * @param EntityManagerInterface $entityManager
32
     * @param UserResolver           $userResolver
33
     */
34 2
    public function __construct(EntityManagerInterface $entityManager, UserResolver $userResolver)
35
    {
36 2
        $this->entityManager = $entityManager;
37 2
        $this->userResolver = $userResolver;
38 2
    }
39
40
    /**
41
     * @param User $profile
42
     *
43
     * @throws \Exception
44
     *
45
     * @return array
46
     */
47 1
    public function __invoke(User $profile)
48
    {
49 1
        $user = $this->userResolver->getCurrentUser();
50 1
        $user->unfollow($profile);
51 1
        $this->entityManager->flush();
52
53 1
        return ['profile' => $profile];
54
    }
55
}
56