SoftwareService.php 2.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
/**
 * gredu_labs
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\Schools\Service;

use InvalidArgumentException;
use RedBeanPHP\R;

class SoftwareService implements SoftwareServiceInterface
{

    public function createSoftwareCategory($name)
    {
        $software_category = R::dispense('softwarecategory');
        $software_category->name = $name;
        R::store($software_category);
    }

    public function getSoftwareCategoryById($id)
    {
        $software_category = R::load('softwarecategory', $id);
        return $software_category->export();
    }

    public function getSoftwareCategories()
    {
        $software_categories = R::findAll('softwarecategory');
34
        return $this->exportAll($software_categories);
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    }

    public function updateSoftwareCategory($id, $data)
    {
        $software_category = R::load('softwarecategory');
        $software_category->name = $name;
        R::store($software_category);
    }

    public function createSoftware(array $data)
    {
        unset($data['id']);
        $software = R::dispense('software');
        $this->persistSoftware($software, $data);
        return $software->export();
    }

    public function updateSoftware(array $data, $id)
    {
        $software = R::load('software', $id);
        if (!$software->id) {
            throw new \InvalidArgumentException('No software found');
        }
        $this->persistSoftware($software, $id);
        return $software->export();
    }

    private function persistSoftware($software, array $data)
    {
        $software->softwarecategory_id = $data['softwarecategory'];
        $software->school_id            = $data['school_id'];
        $software->lab_id               = $data['lab_id'];
        $software->title                = $data['title'];
        $software->vendor               = $data['vendor'];
        $software->url                  = $data['url'];
        
        R::store($software);
    }

    public function getSoftwareById($id)
    {
        $software = R::load('software', $id);
        return $software->export();
    }

    public function getSoftwareBySchoolId($id)
    {
        $software = R::findAll('software', 'school_id = ?', [$id]);
83
        return $this->exportAll($software);
84
85
86
87
88
89
90
    }

    public function getSoftwareByLabId($id)
    {
        $software = R::findAll('software', 'lab_id = ?', [$id]);
        return $software->exportAll();
    }
91

92
93
94
95
96
    public function removeSoftware($id)
    {
        R::trash('software', $id);
    }

97
98
99
100
101
102
103
104
105
    private function exportAll($beans)
    {
        $exported = [];
        foreach($beans as $bean)
        {
            $exported[] = $bean->export();
        }
        return $exported;
    }
106
}