CreateLabs.php 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?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 SchSync\Middleware;

13
use Exception;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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
use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use Psr\Log\LoggerInterface;
use RedBeanPHP\R;
use SchInventory\ServiceInterface as InventoryService;
use Slim\Http\Request;
use Slim\Http\Response;
use Zend\Authentication\AuthenticationServiceInterface;

class CreateLabs
{
    /**
     *
     * @var LabServiceInterface
     */
    protected $labService;

    /**
     *
     * @var AssetServiceInterface
     */
    protected $assetService;

    /**
     *
     * @var InventoryService
     */
    protected $inventoryService;

    /**
     *
     * @var SchoolServiceInterface
     */
    protected $schoolService;

    /**
     *
     * @var AuthenticationServiceInterface
     */
    protected $authService;

    /**
     * @var LoggerInterface
     */
    private $logger;

61 62 63 64 65 66
    /**
     *
     * @var array
     */
    private $categoryMap;

67 68 69 70 71 72
    public function __construct(
        LabServiceInterface $labService,
        AssetServiceInterface $assetService,
        InventoryService $inventoryService,
        SchoolServiceInterface $schoolService,
        AuthenticationServiceInterface $authService,
73 74
        LoggerInterface $logger,
        array $categoryMap = []
75 76 77 78 79 80 81
    ) {
        $this->labService       = $labService;
        $this->assetService     = $assetService;
        $this->inventoryService = $inventoryService;
        $this->schoolService    = $schoolService;
        $this->authService      = $authService;
        $this->logger           = $logger;
82
        $this->categoryMap      = $categoryMap;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    }

    public function __invoke(Request $req, Response $res, callable $next)
    {
        $res = $next($req, $res);

        $identity = $this->authService->getIdentity();
        if (null === $identity) {
            return $res;
        }
        $user = R::load('user', $identity->id);
        if (!$user->school_id) {
            return $res;
        }

        $school_id = $user->school_id;
        $school    = $this->schoolService->getSchool($school_id);

        if (0 < count($this->labService->getLabsBySchoolId($school_id))) {
            return $res;
        }
104 105 106 107 108
        try {
            $equipment = $this->inventoryService->getUnitEquipment($school['registry_no']);
        } catch (Exception $e) {
            $this->logger->error(sprintf('Problem retrieving assets from inventory for school %s', $school_id));
            $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
109

110 111
            return $res;
        }
112 113 114 115 116 117 118 119 120 121 122
        $labTypes  = array_reduce($this->labService->getLabTypes(), function ($map, $type) {
            $map[trim($type['name'])] = $type['id'];

            return $map;
        }, []);
        $assetTypes = array_reduce($this->assetService->getAllItemCategories(), function ($map, $type) {
            $map[trim($type['name'])] = $type['id'];

            return $map;
        }, []);

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        try {
            $locations = array_reduce($equipment, function ($uniq, $item) use ($school_id, $labTypes, $assetTypes) {
                if (!isset($uniq[$item['location.id']])) {
                    $locationName = $item['location.name'];
                    $detected = reset(array_filter(array_keys($labTypes), function ($type) use ($locationName) {

                        return false !== stripos($locationName, $type) ||
                                false !== stripos($type, $locationName);
                    }));
                    $labType = $detected ? $labTypes[$detected] : end($labTypes);
                    $data = [
                        'school_id'  => (int) $school_id,
                        'name'       => $locationName,
                        'labtype_id' => (int) $labType,
                    ];
                    $lab = R::dispense('lab');
                    $lab->import($data);
                    $uniq[$item['location.id']] = $lab;
                }
142

143
                $categoryName = $item['item_template.category.name'];
144

145
                $type = reset(array_filter(array_keys($this->categoryMap), function ($type) use ($categoryName) {
146
                    return $type == $categoryName;
147
                }));
148
                $type = ($type) ? $assetTypes[$this->categoryMap[$type]] : false;
149 150 151 152 153 154 155 156 157

                if ($type !== false) {
                    if (!isset($uniq[$item['location.id']]->ownSchoolAsset[$type])) {
                        $asset = R::dispense('schoolasset');
                        $asset->school_id = (int) $school_id;
                        $asset->itemcategory_id = (int) $type;
                        $uniq[$item['location.id']]->ownSchoolAsset[$type] = $asset;
                    }
                    $uniq[$item['location.id']]->ownSchoolAsset[$type]->qty += 1;
158 159
                }

160 161 162 163 164 165 166 167
                return $uniq;
            }, []);
            R::storeAll($locations);
            $this->logger->info(sprintf('Add assets from inventory for school %s', $school_id));
        } catch (Exception $e) {
            $this->logger->error(sprintf('Problem inserting assets for school %s in database', $school_id));
            $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]);
        }
168 169 170 171

        return $res;
    }
}