Completed
Push — develop ( c2f517...c442c3 )
by
unknown
08:00
created

Cv::createFromApplication()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 8.5806
cc 4
eloc 23
nc 4
nop 2
1
<?php
2
3
namespace Cv\Repository;
4
5
use Core\Repository\AbstractRepository;
6
use Auth\Entity\UserInterface;
7
use Applications\Entity\Application;
8
use Cv\Entity\Cv as CvEntity;
9
10
/**
11
 * class for accessing CVs
12
 *
13
 * @method CvEntity create(array $data = null, $persist = false)
14
 */
15
class Cv extends AbstractRepository
16
{
17
    /**
18
     * Look for an drafted Document of a given user
19
     *
20
     * @param $user
21
     * @return CvEntity|null
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23 View Code Duplication
    public function findDraft($user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        if ($user instanceof UserInterface) {
26
            $user = $user->getId();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $user. This often makes code more readable.
Loading history...
27
        }
28
29
        $document = $this->findOneBy(
30
            array(
31
                'isDraft' => true,
32
                'user' => $user
33
            )
34
        );
35
36
        if (!empty($document)) {
37
            return $document;
38
        }
39
40
        return null;
41
    }
42
    
43
    /**
44
     * @param Application $application
45
     * @param UserInterface $user
46
     * @return CvEntity
47
     * @since 0.26
48
     */
49
    public function createFromApplication(Application $application, UserInterface $user)
50
    {
51
        $cv = $this->create();
52
        $contact = $application->getContact();
53
        $contactImage = $contact->getImage();
54
        
55
        if ($contactImage)
56
        {
57
            $contactImage->setUser($user);
58
        }
59
        
60
        $cv->setContact($contact);
61
        
62
        $applicationAttachments = $application->getAttachments();
63
        
64
        if (count($applicationAttachments) > 0)
65
        {
66
            $cvAttachments = [];
67
        
68
            /* @var $applicationAttachment \Applications\Entity\Attachment */
69
            foreach ($applicationAttachments as $applicationAttachment)
70
            {
71
                $file = new \Doctrine\MongoDB\GridFSFile();
72
                $file->setBytes($applicationAttachment->getContent());
73
                
74
                $cvAttachment = new \Cv\Entity\Attachment();
75
                $cvAttachment->setName($applicationAttachment->getName());
76
                $cvAttachment->setType($applicationAttachment->getType());
77
                $cvAttachment->setPermissions($cvAttachment->getPermissions());
78
                $cvAttachment->setUser($user);
79
                $cvAttachment->setFile($file);
80
                $cvAttachment->setDateUploaded($applicationAttachment->getDateUploaded());
81
                
82
                $cvAttachments[] = $cvAttachment;
83
            }
84
            
85
            $cv->setAttachments(new \Doctrine\Common\Collections\ArrayCollection($cvAttachments));
86
        }
87
        
88
        return $cv;
89
    }
90
}
91