Passed
Push — master ( fe103b...7bb24b )
by Timo
23:28
created

ExtractingQuery::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use Solarium\QueryType\Extract\Query as SolariumExtractQuery;
28
29
/**
30
 * Specialized query for content extraction using Solr Cell
31
 *
32
 */
33
class ExtractingQuery extends SolariumExtractQuery
34
{
35
    protected $multiPartPostDataBoundary;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param string $file Absolute path to the file to extract content and meta data from.
41
     */
42 1
    public function __construct($file)
43
    {
44 1
        parent::__construct();
45 1
        $this->setFile($file);
46 1
        $this->multiPartPostDataBoundary = '--' . md5(uniqid(time()));
47 1
        $this->addParam('extractFormat', 'text');
48 1
    }
49
50
    /**
51
     * Returns the boundary used for this multi-part form-data POST body data.
52
     *
53
     * @return string multi-part form-data POST boundary
54
     */
55 1
    public function getMultiPartPostDataBoundary()
56
    {
57 1
        return $this->multiPartPostDataBoundary;
58
    }
59
60
    /**
61
     * Gets the filename portion of the file.
62
     *
63
     * @return string The filename.
64
     */
65
    public function getFileName()
66
    {
67
        return basename($this->getFile());
68
    }
69
70
    /**
71
     * Constructs a multi-part form-data POST body from the file's content.
72
     *
73
     * @param string $boundary Optional boundary to use
74
     * @return string The file to extract as raw POST data.
75
     * @throws \Apache_Solr_InvalidArgumentException
76
     */
77 1
    public function getRawPostFileData($boundary = '')
78
    {
79 1
        if (empty($boundary)) {
80 1
            $boundary = $this->multiPartPostDataBoundary;
81
        }
82
83 1
        $fileData = file_get_contents($this->getFile());
84 1
        if ($fileData === false) {
85
            throw new \Apache_Solr_InvalidArgumentException('Could not retrieve content from file ' . $this->getFile());
86
        }
87
88 1
        $data = "--{$boundary}\r\n";
89
        // The 'filename' used here becomes the property name in the response.
90 1
        $data .= 'Content-Disposition: form-data; name="file"; filename="extracted"';
91 1
        $data .= "\r\nContent-Type: application/octet-stream\r\n\r\n";
92 1
        $data .= $fileData;
93 1
        $data .= "\r\n--{$boundary}--\r\n";
94
95 1
        return $data;
96
    }
97
}
98