Completed
Push — fix-inspection ( 204a0e...d03297 )
by Yuichi
07:38
created

File   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 74
ccs 27
cts 27
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 1
A post() 0 22 2
A getFilename() 0 8 2
1
<?php
2
3
namespace CybozuHttp\Api\Kintone;
4
5
use CybozuHttp\Client;
6
use CybozuHttp\Api\KintoneApi;
7
use CybozuHttp\Middleware\JsonStream;
8
9
10
/**
11
 * @author ochi51 <[email protected]>
12
 */
13
class File
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20 1
    public function __construct(Client $client)
21
    {
22 1
        $this->client = $client;
23 1
    }
24
25
    /**
26
     * Get file
27
     * https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1
28
     *
29
     * @param string $fileKey
30
     * @param int $guestSpaceId
31
     * @return string
32
     */
33 1
    public function get($fileKey, $guestSpaceId = null)
34
    {
35 1
        $options = ['json' => ['fileKey' => $fileKey]];
36 1
        $response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options);
37
38 1
        return (string)$response->getBody();
39
    }
40
41
    /**
42
     * Post file
43
     * https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1
44
     *
45
     * @param string $filename
46
     * @param int $guestSpaceId
47
     * @return string
48
     */
49 1
    public function post($filename, $guestSpaceId = null)
50
    {
51
        $options = ['multipart' =>  [
52
            [
53 1
                'name' => 'file',
54 1
                'filename' => self::getFilename($filename),
55 1
                'contents' => fopen($filename, 'rb'),
56 1
                'headers' => ['Content-Type' => mime_content_type($filename)]
57 1
            ]
58 1
        ]];
59 1
        $baseUri = $this->client->getConfig('base_uri');
60 1
        if (strpos($baseUri->getHost(), 'cybozu.com') > 0) { // Japanese kintone
61 1
            setlocale(LC_ALL, 'ja_JP.UTF-8');
62 1
        }
63
64
        /** @var JsonStream $stream */
65 1
        $stream = $this->client
66 1
            ->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options)
67 1
            ->getBody();
68
69 1
        return $stream->jsonSerialize()['fileKey'];
70
    }
71
72
    /**
73
     * Returns locale independent base name of the given path.
74
     *
75
     * @param string $name The new file name
76
     * @return string containing
77
     */
78 10
    public static function getFilename($name)
79
    {
80 10
        $originalName = str_replace('\\', '/', $name);
81 10
        $pos = strrpos($originalName, '/');
82 10
        $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
83
84 10
        return $originalName;
85
    }
86
}