MigrateAttendeeFieldsTask   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 3
A publishEvents() 0 13 4
A moveFieldData() 0 29 5
1
<?php
2
/**
3
 * MigrateAttendeeFieldsTask.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 30/05/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use BuildTask;
12
use CalendarEvent;
13
use DB;
14
use Director;
15
use SQLSelect;
16
17
/**
18
 * Class CleanCartTask
19
 * Cleanup discarded tasks
20
 *
21
 * @deprecated after version 1.1.5 the custom fields where moved to UserFields.
22
 * @package Broarm\EventTickets
23
 */
24
class MigrateAttendeeFieldsTask extends BuildTask
25
{
26
    protected $title = 'Migrate attendee fields task';
27
28
    protected $description = 'Moves the attendee fields from the model to relations. This enables the custom fields feature from version 1.1.5';
29
30
    protected $enabled = false;
31
32
    /**
33
     * @param \SS_HTTPRequest $request
34
     */
35
    public function run($request)
36
    {
37
        if (!Director::is_cli()) echo '<pre>';
38
        echo "Start migration\n\n";
39
40
        $this->publishEvents();
41
42
        echo "Finished migration task \n";
43
        if (!Director::is_cli()) echo '</pre>';
44
    }
45
46
    /**
47
     * Publishes the events so the default fields are created
48
     */
49
    private function publishEvents() {
50
        /** @var CalendarEvent|TicketExtension $event */
51
        foreach (CalendarEvent::get() as $event) {
52
            if ($event->Tickets()->exists()) {
53
                if ($event->doPublish()) {
54
                    echo "[$event->ID] Published event \n";
55
                    $this->moveFieldData($event);
56
                } else {
57
                    echo "[$event->ID] Failed to publish event \n\n";
58
                }
59
            }
60
        }
61
    }
62
63
    /**
64
     * Migrate the field data from the attendee model to relations
65
     * Use an SQLSelect to access the data directly from the database
66
     *
67
     * @param CalendarEvent|TicketExtension $event
68
     */
69
    private function moveFieldData(CalendarEvent $event) {
70
        if ($event->Attendees()->exists()) {
71
            /** @var Attendee $attendee */
72
            foreach ($event->Attendees() as $attendee) {
73
                /** @var UserField $field */
74
                foreach ($event->Fields() as $field) {
75
                    $q = SQLSelect::create(
76
                        $field->FieldName,
77
                        "`{$attendee->getClassName()}`",
78
                        array('ID' => $attendee->ID)
79
                    );
80
81
                    try {
82
                        $value = $q->execute()->value();
83
                        $attendee->Fields()->add($field, array(
84
                            'Value' => $value
85
                        ));
86
                        echo "[$event->ID][$attendee->ID] Set '$field->FieldName' with '{$value}' \n";
87
                    } catch (\Exception $e) {
88
                        // fails silent
89
                        echo "[$event->ID][$attendee->ID] Failed, '$field->FieldName' does not exist \n";
90
                    }
91
                }
92
            }
93
            echo "[$event->ID] Finished migrating event \n\n";
94
        } else {
95
            echo "[$event->ID] No attendees to migrate \n\n";
96
        }
97
    }
98
}
99