~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/TaskFactory.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2016 Spring Signage Ltd
5
 
 * (TaskFactory.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Factory;
10
 
use Xibo\Entity\Task;
11
 
use Xibo\Exception\NotFoundException;
12
 
use Xibo\Service\LogServiceInterface;
13
 
use Xibo\Service\SanitizerServiceInterface;
14
 
use Xibo\Storage\StorageServiceInterface;
15
 
 
16
 
/**
17
 
 * Class TaskFactory
18
 
 * @package Xibo\Factory
19
 
 */
20
 
class TaskFactory extends BaseFactory
21
 
{
22
 
    /**
23
 
     * Construct a factory
24
 
     * @param StorageServiceInterface $store
25
 
     * @param LogServiceInterface $log
26
 
     * @param SanitizerServiceInterface $sanitizerService
27
 
     */
28
 
    public function __construct($store, $log, $sanitizerService)
29
 
    {
30
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
31
 
    }
32
 
 
33
 
    /**
34
 
     * Create empty
35
 
     * @return Task
36
 
     */
37
 
    public function create()
38
 
    {
39
 
        return new Task($this->getStore(), $this->getLog());
40
 
    }
41
 
 
42
 
    /**
43
 
     * Get by ID
44
 
     * @param int $taskId
45
 
     * @return Task
46
 
     * @throws NotFoundException if the task cannot be resolved from the provided route
47
 
     */
48
 
    public function getById($taskId)
49
 
    {
50
 
        $tasks = $this->query(null, array('taskId' => $taskId));
51
 
 
52
 
        if (count($tasks) <= 0)
53
 
            throw new NotFoundException();
54
 
 
55
 
        return $tasks[0];
56
 
    }
57
 
 
58
 
    /**
59
 
     * Get by Name
60
 
     * @param string $task
61
 
     * @return Task
62
 
     * @throws NotFoundException if the task cannot be resolved from the provided route
63
 
     */
64
 
    public function getByName($task)
65
 
    {
66
 
        $tasks = $this->query(null, array('name' => $task));
67
 
 
68
 
        if (count($tasks) <= 0)
69
 
            throw new NotFoundException();
70
 
 
71
 
        return $tasks[0];
72
 
    }
73
 
 
74
 
    /**
75
 
     * Get by Class
76
 
     * @param string $class
77
 
     * @return Task
78
 
     * @throws NotFoundException if the task cannot be resolved from the provided route
79
 
     */
80
 
    public function getByClass($class)
81
 
    {
82
 
        $tasks = $this->query(null, array('class' => $class));
83
 
 
84
 
        if (count($tasks) <= 0)
85
 
            throw new NotFoundException();
86
 
 
87
 
        return $tasks[0];
88
 
    }
89
 
 
90
 
    /**
91
 
     * @param null $sortOrder
92
 
     * @param array $filterBy
93
 
     * @return array
94
 
     */
95
 
    public function query($sortOrder = null, $filterBy = [])
96
 
    {
97
 
        if ($sortOrder == null)
98
 
            $sortOrder = ['name'];
99
 
 
100
 
        $entries = array();
101
 
        $params = array();
102
 
        $sql = '
103
 
          SELECT `taskId`, `name`, `status`, `pid`, `configFile`, `class`, `options`, `schedule`, 
104
 
              `lastRunDt`, `lastRunStatus`, `lastRunMessage`, `lastRunDuration`, `lastRunExitCode`,
105
 
              `isActive`, `runNow`
106
 
        ';
107
 
 
108
 
        if (DBVERSION >= 133)
109
 
            $sql .= ', `lastRunStartDt` ';
110
 
 
111
 
        $sql .= '
112
 
            FROM `task` 
113
 
           WHERE 1 = 1 
114
 
        ';
115
 
 
116
 
        if ($this->getSanitizer()->getString('name', $filterBy) != null) {
117
 
            $params['name'] = $this->getSanitizer()->getString('name', $filterBy);
118
 
            $sql .= ' AND `name` = :name ';
119
 
        }
120
 
 
121
 
        if ($this->getSanitizer()->getString('class', $filterBy) != null) {
122
 
            $params['class'] = $this->getSanitizer()->getString('class', $filterBy);
123
 
            $sql .= ' AND `class` = :class ';
124
 
        }
125
 
 
126
 
        if ($this->getSanitizer()->getInt('taskId', $filterBy) !== null) {
127
 
            $params['taskId'] = $this->getSanitizer()->getString('taskId', $filterBy);
128
 
            $sql .= ' AND `taskId` = :taskId ';
129
 
        }
130
 
 
131
 
        // Sorting?
132
 
        $sql .= 'ORDER BY ' . implode(',', $sortOrder);
133
 
 
134
 
 
135
 
        foreach ($this->getStore()->select($sql, $params) as $row) {
136
 
            $task = $this->create()->hydrate($row, [
137
 
                'intProperties' => [
138
 
                    'status', 'lastRunStatus', 'nextRunDt', 'lastRunDt', 'lastRunStartDt', 'lastRunExitCode', 'runNow', 'isActive', 'pid'
139
 
                ]
140
 
            ]);
141
 
 
142
 
            if ($task->options != null)
143
 
                $task->options = json_decode($task->options, true);
144
 
            else
145
 
                $task->options = [];
146
 
 
147
 
            $entries[] = $task;
148
 
        }
149
 
 
150
 
        return $entries;
151
 
    }
152
 
}
 
 
b'\\ No newline at end of file'