Completed
Pull Request — master (#144)
by Markus
01:01
created

EventFactory::createComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\GeoValue;
25
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
26
use Generator;
27
28
class EventFactory
29
{
30
    public function createComponent(Event $event): Component
31
    {
32
        return Component::create('VEVENT', iterator_to_array($this->getProperties($event), false));
33
    }
34
35
    /**
36
     * @return Generator<Property>
0 ignored issues
show
Documentation introduced by
The doc-type Generator<Property> could not be parsed: Expected "|" or "end of type", but got "<" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
37
     */
38
    private function getProperties(Event $event): Generator
39
    {
40
        yield Property::create('UID', TextValue::fromString((string) $event->getUniqueIdentifier()));
41
        yield Property::create('DTSTAMP', DateTimeValue::fromTimestamp($event->getTouchedAt()));
42
43
        if ($event->hasSummary()) {
44
            yield Property::create('SUMMARY', TextValue::fromString($event->getSummary()));
45
        }
46
47
        if ($event->hasDescription()) {
48
            yield Property::create('DESCRIPTION', TextValue::fromString($event->getDescription()));
49
        }
50
51
        if ($event->hasOccurrence()) {
52
            yield from $this->getOccurrenceProperties($event->getOccurrence());
53
        }
54
55
        if ($event->hasLocation()) {
56
            yield from $this->getLocationProperties($event);
57
        }
58
    }
59
60
    /**
61
     * @return Generator<Property>
0 ignored issues
show
Documentation introduced by
The doc-type Generator<Property> could not be parsed: Expected "|" or "end of type", but got "<" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     */
63
    private function getOccurrenceProperties(Occurrence $occurrence): Generator
64
    {
65
        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...
66
            yield Property::create('DTSTART', DateValue::fromDate($occurrence->getDate()));
67
        }
68
69
        if ($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...
70
            yield Property::create('DTSTART', DateValue::fromDate($occurrence->getFirstDay()));
71
            yield Property::create('DTEND', DateValue::fromDate($occurrence->getLastDay()->add(new DateInterval('P1D'))));
72
        }
73
74
        if ($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...
75
            yield Property::create('DTSTART', DateTimeValue::fromDateTime($occurrence->getBegin()));
76
            yield Property::create('DTEND', DateTimeValue::fromDateTime($occurrence->getEnd()));
77
        }
78
    }
79
80
    /**
81
     * @return Generator<Property>
0 ignored issues
show
Documentation introduced by
The doc-type Generator<Property> could not be parsed: Expected "|" or "end of type", but got "<" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
82
     */
83
    private function getLocationProperties(Event $event): Generator
84
    {
85
        yield Property::create('LOCATION', TextValue::fromString((string) $event->getLocation()));
86
87
        if ($event->getLocation()->hasGeographicalPosition()) {
88
            yield Property::create('GEO', GeoValue::fromGeographicPosition($event->getLocation()->getGeographicPosition()));
89
        }
90
    }
91
}
92