/
Method.php
139 lines (121 loc) · 3.32 KB
/
Method.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* This file is part of the Eko\GoogleTranslateBundle Symfony bundle.
*
* (c) Vincent Composieux <vincent.composieux@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eko\GoogleTranslateBundle\Translate;
use GuzzleHttp\Client as GuzzleClient;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;
/**
* Class Method.
*
* This is the main Method class that is instancing Guzzle HTTP client
*
* @author Vincent Composieux <vincent.composieux@gmail.com>
*/
class Method
{
/**
* @var string Google Translate API key
*/
protected $apiKey = null;
/**
* @var GuzzleClient A Guzzle client instance
*/
protected $client;
/**
* @var string API translation url
*/
protected $url = null;
/**
* @var array Symfony profiler profiles data
*/
protected $profiles = [];
/**
* @var Stopwatch Symfony profiler Stopwatch service
*/
protected $stopwatch;
/**
* @var int
*/
protected $counter = 1;
/**
* Method constructor.
*
* @param string $apiKey API key retrieved from configuration
* @param GuzzleClient $client
* @param Stopwatch $stopwatch Symfony profiler Stopwatch service
*/
public function __construct($apiKey, GuzzleClient $client, Stopwatch $stopwatch = null)
{
$this->apiKey = $apiKey;
$this->client = $client;
$this->stopwatch = $stopwatch;
}
/**
* Returns Guzzle HTTP client instance.
*
* @return GuzzleClient
*/
public function getClient()
{
return $this->client;
}
/**
* Returns profiled data.
*
* @return array
*/
public function getProfiles()
{
return $this->profiles;
}
/**
* Starts profiling.
*
* @param string $name Method name
* @param string $query Query text
* @param string $source Source language
* @param string $target Target language
*
* @return StopwatchEvent
*/
protected function startProfiling($name, $query, $source = null, $target = null)
{
if ($this->stopwatch instanceof Stopwatch) {
$this->profiles[$this->counter] = [
'query' => urldecode($query),
'source' => $source,
'target' => $target,
'duration' => null,
'memory_start' => memory_get_usage(true),
'memory_end' => null,
'memory_peak' => null,
];
return $this->stopwatch->start($name);
}
}
/**
* Stops the profiling.
*
* @param StopwatchEvent $event A stopwatchEvent instance
*/
protected function stopProfiling(StopwatchEvent $event = null)
{
if ($this->stopwatch instanceof Stopwatch) {
$event->stop();
$values = [
'duration' => $event->getDuration(),
'memory_end' => memory_get_usage(true),
'memory_peak' => memory_get_peak_usage(true),
];
$this->profiles[$this->counter] = array_merge($this->profiles[$this->counter], $values);
$this->counter++;
}
}
}