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