Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.00% covered (warning)
76.00%
19 / 25
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sgf
76.00% covered (warning)
76.00%
19 / 25
50.00% covered (danger)
50.00%
1 / 2
11.38
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 uploadSgf
73.91% covered (warning)
73.91%
17 / 23
0.00% covered (danger)
0.00%
0 / 1
10.44
1<?php
2
3class Sgf extends AppModel
4{
5    public $validate = [
6        'tsumego_id' => [
7            'required' => [
8                'rule' => 'numeric',
9                'message' => 'Tsumego id is required',
10                'allowEmpty' => false,
11                'required' => true,
12            ],
13        ],
14        'sgf' => [
15            'notBlank' => [
16                'rule' => 'notBlank',
17                'message' => 'SGF content is required',
18                'allowEmpty' => false,
19                'required' => true,
20            ],
21        ],
22    ];
23
24    public function __construct($id = false, $table = null, $ds = null)
25    {
26        $id['table'] =  'sgf';
27        parent::__construct($id, $table, $ds);
28    }
29
30    /**
31     * Uploads and saves SGF data for a tsumego
32     *
33     * @param string|array $sgfDataOrFile The SGF content string, or $_FILES['fieldName'] array
34     * @param int $tsumegoID The tsumego ID to attach the SGF to
35     * @param int $userID The user ID uploading the SGF
36     * @param bool $accepted Whether the SGF is accepted (true for admin uploads)
37     * @return array The saved Sgf record
38     * @throws AppException If validation fails
39     */
40    public function uploadSgf($sgfDataOrFile, int $tsumegoID, int $userID, bool $accepted): array
41    {
42        // If it's a file upload array, extract the SGF data
43        if (is_array($sgfDataOrFile))
44        {
45            if (!isset($sgfDataOrFile['tmp_name']) || $sgfDataOrFile['error'] !== UPLOAD_ERR_OK)
46                throw new AppException('Invalid file upload.');
47
48            $fileSize = $sgfDataOrFile['size'];
49            $array1 = explode('.', $sgfDataOrFile['name']);
50            $fileExtension = strtolower(end($array1));
51            if ($fileExtension != 'sgf')
52                throw new AppException('Only SGF files are allowed, the file name is: ' . $sgfDataOrFile['name']);
53            if ($fileSize > 2097152)
54                throw new AppException('The file is too large.');
55
56            $sgfData = file_get_contents($sgfDataOrFile['tmp_name']);
57        }
58        else
59            $sgfData = $sgfDataOrFile;
60
61        $this->create();
62        $sgf = [];
63        $sgf['sgf'] = $sgfData;
64        $sgf['user_id'] = $userID;
65        $sgf['tsumego_id'] = $tsumegoID;
66        $sgf['accepted'] = $accepted;
67        if (!$this->save($sgf))
68        {
69            $errorMessages = array_map(function ($error) { return is_array($error) ? implode(' ', $error) : $error; }, $this->validationErrors);
70            $errorMessage = empty($errorMessages) ? 'validation failed.' : implode('; ', $errorMessages);
71            throw new AppException('Failed to save SGF: ' . $errorMessage);
72        }
73
74        // Return the saved record from the model (includes auto-generated ID and timestamps)
75        return ['Sgf' => $this->data['Sgf']];
76    }
77}