Completed
Push — master ( cdfd9f...76eeb1 )
by
unknown
03:29
created

Container::preDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Attachment\Object;
4
5
// From Pimple
6
use Pimple\Container as ServiceContainer;
7
8
// From 'charcoal-config'
9
use Charcoal\Config\ConfigurableInterface;
10
11
// From 'charcoal-attachment'
12
use Charcoal\Attachment\Interfaces\AttachmentAwareInterface;
13
use Charcoal\Attachment\Interfaces\AttachmentContainerInterface;
14
use Charcoal\Attachment\Traits\AttachmentAwareTrait;
15
use Charcoal\Attachment\Traits\AttachmentContainerTrait;
16
use Charcoal\Attachment\Traits\ConfigurableAttachmentsTrait;
17
18
/**
19
 * Gallery Attachment Type
20
 *
21
 * This type allows for nesting of additional attachment types.
22
 */
23
class Container extends Attachment implements
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: hasProperty, p, properties, property
Loading history...
24
    AttachmentAwareInterface,
25
    AttachmentContainerInterface,
26
    ConfigurableInterface
27
{
28
    use AttachmentAwareTrait;
29
    use AttachmentContainerTrait;
30
    use ConfigurableAttachmentsTrait;
31
32
    /**
33
     * Inject dependencies from a DI Container.
34
     *
35
     * @param  ServiceContainer $container A dependencies container instance.
36
     * @return void
37
     */
38 View Code Duplication
    protected function setDependencies(ServiceContainer $container)
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...
39
    {
40
        parent::setDependencies($container);
41
42
        if (isset($container['attachments/config'])) {
43
            $this->setConfig($container['attachments/config']);
44
        } elseif (isset($container['config']['attachments'])) {
45
            $this->setConfig($container['config']['attachments']);
46
        }
47
    }
48
49
    /**
50
     * Retrieve the objects associated to the current object.
51
     *
52
     * @param  mixed ...$args Filter the attachments;
53
     *     options accepted by {@see AttachmentAwareTrait::attachments()}.
54
     * @return Collection|Attachment[]
55
     */
56
    public function attachments(...$args)
57
    {
58
        $attachables = $this->attachableObjects();
59
        $attachments = call_user_func_array([ $this, 'getAttachments' ], $args);
60
61
        foreach ($attachments as $attachment) {
62
            if (isset($attachables[$attachment->objType()])) {
63
                $attachment->attachmentType = $attachables[$attachment->objType()];
64
            } else {
65
                $attachment->attachmentType = [];
66
            }
67
        }
68
69
        return $attachments;
70
    }
71
72
    /**
73
     * Event called before _deleting_ the attachment.
74
     *
75
     * @return boolean
76
     * @see    Charcoal\Attachment\Traits\AttachmentAwareTrait::removeJoins
77
     * @see    Charcoal\Source\StorableTrait::preDelete() For the "create" Event.
78
     */
79
    public function preDelete()
80
    {
81
        // Delete nested attachments
82
        array_map(function($attachment) {
83
            $attachment->delete();
84
        }, $this->attachments()->values());
85
86
        return parent::preDelete();
87
    }
88
}
89