Completed
Push — master ( 666f26...e51d97 )
by Florian
02:35
created

Downloader::download()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.4285
cc 3
eloc 13
nc 4
nop 4
crap 3
1
<?php
2
3
namespace Laposte\DatanovaBundle\Service;
4
5
use Laposte\DatanovaBundle\Client\ClientInterface;
6
7
class Downloader
8
{
9
    /** @var ClientInterface $client */
10
    private $client;
11
12
    /** @var  Finder $finder */
13
    private $finder;
14
15
    /**
16
     * @param ClientInterface $client
17
     * @param Finder $finder
18
     */
19 2
    public function __construct(ClientInterface $client, Finder $finder)
20
    {
21 2
        $this->client = $client;
22 2
        $this->finder = $finder;
23 2
    }
24
25
    /**
26
     * @param string $dataset
27
     * @param string $format
28
     * @param string $filter
29
     * @param bool $updateExisting
30
     *
31
     * @return false|string downloaded file content
32
     */
33 1
    public function download($dataset, $format, $filter = null, $updateExisting = false)
34
    {
35 1
        $result = false;
36
        $parameters = array(
37 1
            'dataset' => $dataset,
38
            'format' => $format
39 1
        );
40 1
        if (isset($filter)) {
41 1
            $parameters['q'] = $filter;
42 1
        }
43 1
        $this->client->setTimeout(0);
44 1
        $content = $this->client->get('download', $parameters);
45 1
        $save = $this->finder->save($dataset, $content, $format, $filter, $updateExisting);
46 1
        if (false !== $save) {
47 1
            $result = $this->finder->getContent($save);
48 1
        }
49
50 1
        return $result;
51
    }
52
53
    /**
54
     * @param string $dataset
55
     * @param string $format
56
     * @param string $filter
57
     * @return false|string
58
     */
59 1
    public function findDownload($dataset, $format, $filter = null)
60
    {
61 1
        return $this->finder->findDataset($dataset, $format, $filter);
62
    }
63
}
64