Completed
Pull Request — master (#144)
by Markus
02:49 queued 01:51
created

EventFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createComponent() 0 4 1
A getProperties() 0 17 4
A getOccurrenceProperties() 0 12 4
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) 2019 Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Presentation\Factory;
13
14
use DateInterval;
15
use Eluceo\iCal\Domain\Entity\Event;
16
use Eluceo\iCal\Domain\ValueObject\MultiDay;
17
use Eluceo\iCal\Domain\ValueObject\Occurrence;
18
use Eluceo\iCal\Domain\ValueObject\SingleDay;
19
use Eluceo\iCal\Domain\ValueObject\TimeSpan;
20
use Eluceo\iCal\Presentation\Component;
21
use Eluceo\iCal\Presentation\Component\Property;
22
use Eluceo\iCal\Presentation\Component\Property\Value\DateTimeValue;
23
use Eluceo\iCal\Presentation\Component\Property\Value\DateValue;
24
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
25
use Generator;
26
27
class EventFactory
28
{
29
    public function createComponent(Event $event): Component
30
    {
31
        return Component::create('VEVENT', iterator_to_array($this->getProperties($event)));
32
    }
33
34
    private function getProperties(Event $event): Generator
35
    {
36
        yield Property::create('UID', TextValue::fromString((string) $event->getUniqueIdentifier()));
37
        yield Property::create('DTSTAMP', DateTimeValue::fromTimestamp($event->getTouchedAt()));
38
39
        if ($event->hasSummary()) {
40
            yield Property::create('SUMMARY', TextValue::fromString($event->getSummary()));
41
        }
42
43
        if ($event->hasDescription()) {
44
            yield Property::create('DESCRIPTION', TextValue::fromString($event->getDescription()));
45
        }
46
47
        if ($event->hasOccurrence()) {
48
            yield from $this->getOccurrenceProperties($event->getOccurrence());
49
        }
50
    }
51
52
    private function getOccurrenceProperties(Occurrence $occurrence): Generator
53
    {
54
        if ($occurrence instanceof SingleDay) {
0 ignored issues
show
Bug introduced by
The class Eluceo\iCal\Domain\ValueObject\SingleDay does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
55
            yield Property::create('DTSTART', DateValue::fromDate($occurrence->getDate()));
56
        } elseif ($occurrence instanceof MultiDay) {
0 ignored issues
show
Bug introduced by
The class Eluceo\iCal\Domain\ValueObject\MultiDay does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
            yield Property::create('DTSTART', DateValue::fromDate($occurrence->getFirstDay()));
58
            yield Property::create('DTEND', DateValue::fromDate($occurrence->getLastDay()->add(new DateInterval('P1D'))));
59
        } elseif ($occurrence instanceof TimeSpan) {
0 ignored issues
show
Bug introduced by
The class Eluceo\iCal\Domain\ValueObject\TimeSpan does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
            yield Property::create('DTSTART', DateTimeValue::fromDateTime($occurrence->getBegin()));
61
            yield Property::create('DTEND', DateTimeValue::fromDateTime($occurrence->getEnd()));
62
        }
63
    }
64
}
65