Passed
Push — master ( eec1ac...56c06a )
by Andreas
26:07
created

org_openpsa_calendar_vcal   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 98.11%

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 102
ccs 52
cts 53
cp 0.9811
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add_event() 0 30 3
A __construct() 0 6 1
A __toString() 0 3 1
A _add_participants() 0 16 3
A _add_date_fields() 0 23 1
1
<?php
2
/**
3
 * @package org.openpsa.calendar
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use Sabre\VObject\Component\VEvent;
10
use Sabre\VObject\Component\VCalendar;
11
12
/**
13
 * vCalendar helper class
14
 *
15
 * @package org.openpsa.calendar
16
 */
17
class org_openpsa_calendar_vcal
18
{
19
    /**
20
     * The calendar object
21
     *
22
     * @var Sabre\VObject\Component\VCalendar
23
     */
24
    private $_calendar;
25
26
    /**
27
     * @param string $method vCalendar method (defaults to "publish")
28
     */
29 1
    public function __construct(string $method = "PUBLISH")
30
    {
31 1
        $this->_calendar = new VCalendar;
32 1
        $this->_calendar->VERSION = '2.0';
33 1
        $this->_calendar->PRODID = "-//OpenPSA//Calendar " . org_openpsa_core_version::get_version_number() . "//" . strtoupper(midcom::get()->i18n->get_current_language());
34 1
        $this->_calendar->METHOD = strtoupper($method);
35 1
    }
36
37
    /**
38
     * Export event in vCalendar format
39
     */
40 1
    public function add_event(org_openpsa_calendar_event_dba $event)
41
    {
42 1
        $vevent = $this->_calendar->createComponent('VEVENT');
43
44
        // TODO: handle UID smarter
45 1
        $vevent->UID = "{$event->guid}-midgardGuid";
46
47 1
        $this->_add_date_fields($vevent, $event);
48
49
        // Type handling
50 1
        if ($event->orgOpenpsaAccesstype === org_openpsa_core_acl::ACCESS_PUBLIC) {
51 1
            $vevent->{'CLASS'} = 'PUBLIC';
52
        } else {
53
            $vevent->{'CLASS'} = 'PRIVATE';
54
        }
55
        // "busy" or "transparency" as vCalendar calls it
56 1
        $vevent->TRANSP = ($event->busy) ? 'OPAQUE' : 'TRANSPARENT';
57
        // tentative vs confirmed
58 1
        $vevent->STATUS = 'CONFIRMED';
59
        // we don't categorize events, at least yet
60 1
        $vevent->CATEGORIES = 'MEETING';
61
        // we don't handle priorities
62 1
        $vevent->PRIORITY = 1;
63
        // Basic fields
64 1
        $vevent->SUMMARY = $event->title;
65 1
        $vevent->DESCRIPTION = $event->description;
66 1
        $vevent->LOCATION = $event->location;
67
68 1
        $this->_add_participants($vevent, $event->participants);
69 1
        $this->_calendar->add($vevent);
70 1
    }
71
72 1
    private function _add_participants(VEvent $vevent, array $participants)
73
    {
74 1
        $participants = array_filter($participants);
75 1
        foreach (array_keys($participants) as $uid) {
76 1
            $person = midcom_db_person::get_cached($uid);
77 1
            if (empty($person->email)) {
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
                // Attendee must have email address of valid format, these must also be unique.
79 1
                $person->email = preg_replace('/[^0-9_a-z]/i', '_', strtolower($person->name)) . '[email protected]';
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on midcom_core_dbaobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
80
            }
81
            $parameters = [
82 1
                'ROLE' => 'REQ-PARTICIPANT',
83 1
                'CUTYPE' => 'INDIVIDUAL',
84 1
                'PARTSTAT' => 'ACCEPTED',
85 1
                'CN' => $person->rname,
0 ignored issues
show
Bug Best Practice introduced by
The property rname does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
            ];
87 1
            $vevent->add('ATTENDEE', "mailto:{$person->email}", $parameters);
88
        }
89 1
    }
90
91 1
    private function _add_date_fields(VEvent $vevent, org_openpsa_calendar_event_dba $event)
92
    {
93 1
        $tz = new DateTimeZone('UTC');
94 1
        $revised = new DateTime('@' . $event->metadata->revised);
95 1
        $revised->setTimezone($tz);
96 1
        $created = new DateTime('@' . $event->metadata->created);
97 1
        $created->setTimezone($tz);
98 1
        $start = new DateTime('@' . $event->start);
99 1
        $start->setTimezone($tz);
100 1
        $end = new DateTime('@' . $event->end);
101 1
        $end->setTimezone($tz);
102
103 1
        $vevent->CREATED = $created;
104
        /**
105
         * The real meaning of the DTSTAMP is fuzzy at best
106
         * http://www.kanzaki.com/docs/ical/dtstamp.html is less than helpful
107
         * http://lists.osafoundation.org/pipermail/ietf-calsify/2007-July/001750.html
108
         * seems to suggest that using the revision would be best
109
         */
110 1
        $vevent->DTSTAMP = $revised;
111 1
        $vevent->{'LAST-MODIFIED'} = $revised;
112 1
        $vevent->DTSTART = $start;
113 1
        $vevent->DTEND = $end;
114 1
    }
115
116 1
    public function __toString()
117
    {
118 1
        return $this->_calendar->serialize();
119
    }
120
}
121