OrganizationManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B createOrganization() 0 22 5
A __construct() 0 4 1
1
<?php
2
3
namespace App\Manager;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
use App\Utils\Slugger;
8
9
use App\Entity\Organization;
10
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13
class OrganizationManager
14
{
15
    /** @var EntityManagerInterface **/
16
    protected $em;
17
    /** @var Slugger **/
18
    protected $slugger;
19
20
    /**
21
     * @param EntityManagerInterface $em
22
     * @param Slugger $slugger
23
     */
24 2
    public function __construct(EntityManagerInterface $em, Slugger $slugger)
25
    {
26 2
        $this->em = $em;
27 2
        $this->slugger = $slugger;
28 2
    }
29
30
    /**
31
     * @param array $data
32
	 * @return Organization
33
     */
34 2
    public function createOrganization(array $data): Organization
35
    {
36 2
		if (empty($data['name'])) {
37
			throw new BadRequestHttpException('organizations.invalid_name');
38
		}
39 2
		if (empty($data['type']) || !in_array($data['type'], Organization::getTypes())) {
40
			throw new BadRequestHttpException('organizations.invalid_type');
41
		}
42 2
		if (!isset($data['description'])) {
43
			throw new BadRequestHttpException('organization.invalid_description');
44
		}
45
		$organization =
46 2
			(new Organization())
47 2
			->setName($data['name'])
48 2
			->setSlug($this->slugger->slugify($data['name']))
49 2
            ->setType($data['type'])
50 2
			->setDescription($data['description'])
51
		;
52 2
        $this->em->persist($organization);
53 2
        $this->em->flush();
54
		
55 2
		return $organization;
56
    }
57
}
58