FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AgentLicenseEventProcessor.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
24 
32 
38 {
41  private $latestAgentMapCache = array();
44  private $licenseDao;
47  private $agentDao;
48 
55  {
56  $this->licenseDao = $licenseDao;
57  $this->agentDao = $agentDao;
58  }
59 
66  public function getScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
67  {
68  $details = $this->getScannerDetectedLicenseDetails($itemTreeBounds, $usageId);
69 
70  return $this->getScannedLicenses($details);
71  }
72 
93  protected function getScannerDetectedLicenseDetails(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
94  {
95  $agentDetectedLicenses = array();
96 
97  $licenseFileMatches = $this->licenseDao->getAgentFileLicenseMatches($itemTreeBounds, $usageId);
98 
99  foreach ($licenseFileMatches as $licenseMatch) {
100  $licenseRef = $licenseMatch->getLicenseRef();
101  $licenseId = $licenseRef->getId();
102  if ($licenseRef->getShortName() === "No_license_found") {
103  continue;
104  }
105  $agentRef = $licenseMatch->getAgentRef();
106  $agentName = $agentRef->getAgentName();
107  $agentId = $agentRef->getAgentId();
108 
109  $agentDetectedLicenses[$agentName][$agentId][$licenseId][] = array(
110  'id' => $licenseId,
111  'licenseRef' => $licenseRef,
112  'agentRef' => $agentRef,
113  'matchId' => $licenseMatch->getLicenseFileId(),
114  'percentage' => $licenseMatch->getPercentage()
115  );
116  }
117 
118  return $this->filterLatestScannerDetectedMatches($agentDetectedLicenses, $itemTreeBounds->getUploadId());
119  }
120 
126  public function getLatestScannerDetectedMatches(ItemTreeBounds $itemTreeBounds)
127  {
128  $agentDetectedLicenses = array();
129 
130  $licenseFileMatches = $this->licenseDao->getAgentFileLicenseMatches($itemTreeBounds);
131 
132  foreach ($licenseFileMatches as $licenseMatch) {
133  $licenseRef = $licenseMatch->getLicenseRef();
134  $licenseId = $licenseRef->getId();
135  if ($licenseRef->getShortName() === "No_license_found") {
136  continue;
137  }
138  $agentRef = $licenseMatch->getAgentRef();
139  $agentName = $agentRef->getAgentName();
140  $agentId = $agentRef->getAgentId();
141 
142  $agentDetectedLicenses[$agentName][$agentId][$licenseId][] = $licenseMatch;
143  }
144 
145  return $this->filterLatestScannerDetectedMatches($agentDetectedLicenses, $itemTreeBounds->getUploadId());
146  }
147 
154  protected function filterLatestScannerDetectedMatches($agentDetectedLicenses, $uploadId)
155  {
156  $agentNames = array_keys($agentDetectedLicenses);
157  if (empty($agentNames)) {
158  return array();
159  }
160 
161  $latestAgentIdPerAgent = $this->getLatestAgentIdPerAgent($uploadId, $agentNames);
162  $latestAgentDetectedLicenses = $this->filterDetectedLicenses($agentDetectedLicenses, $latestAgentIdPerAgent);
163  return $latestAgentDetectedLicenses;
164  }
165 
174  private function getLatestAgentIdPerAgent($uploadId, $agentNames)
175  {
176  if (!array_key_exists($uploadId,$this->latestAgentMapCache)
177  || count(array_diff_key($agentNames, $this->latestAgentMapCache[$uploadId]))>0) {
178  $latestScannerProxy = new LatestScannerProxy($uploadId, $agentNames, "latest_scanner$uploadId");
179  $latestAgentIdPerAgent = $latestScannerProxy->getNameToIdMap();
180  foreach ($latestAgentIdPerAgent as $agentName=>$agentMap) {
181  $this->latestAgentMapCache[$uploadId][$agentName] = $agentMap;
182  }
183  }
184  if (array_key_exists($uploadId, $this->latestAgentMapCache)) {
185  return $this->latestAgentMapCache[$uploadId];
186  } else {
187  return array();
188  }
189  }
190 
197  protected function filterDetectedLicenses($agentDetectedLicenses, $agentLatestMap)
198  {
199  $latestAgentDetectedLicenses = array();
200 
201  foreach ($agentDetectedLicenses as $agentName => $licensesFoundPerAgentId) {
202  if (!array_key_exists($agentName, $agentLatestMap)) {
203  continue;
204  }
205  $latestAgentId = $agentLatestMap[$agentName];
206  if (!array_key_exists($latestAgentId, $licensesFoundPerAgentId)) {
207  continue;
208  }
209  foreach ($licensesFoundPerAgentId[$latestAgentId] as $licenseId => $properties) {
210  $latestAgentDetectedLicenses[$licenseId][$agentName] = $properties;
211  }
212  }
213 
214  return $latestAgentDetectedLicenses;
215  }
216 
222  public function getScannedLicenses($details)
223  {
224  $licenses = array();
225 
226  foreach ($details as $licenseId => $agentEntries) {
227  foreach ($agentEntries as $matchProperties) {
228  $licenses[$licenseId] = $matchProperties[0]['licenseRef'];
229  break;
230  }
231  }
232 
233  return $licenses;
234  }
235 
242  public function getScannerEvents(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
243  {
244  $agentDetails = $this->getScannerDetectedLicenseDetails($itemTreeBounds, $usageId);
245 
246  $result = array();
247  foreach ($agentDetails as $licenseId => $properties) {
248  $agentClearingEvents = array();
249  foreach ($properties as $licenseProperties) {
250  foreach ($licenseProperties as $licenseProperty) {
251  $agentClearingEvents[] = $this->createAgentClearingEvent($licenseProperty);
252  }
253  }
254 
255  $result[$licenseId] = $agentClearingEvents;
256  }
257  return $result;
258  }
259 
266  private function createAgentClearingEvent($licenseProperty)
267  {
268  return new AgentClearingEvent(
269  $licenseProperty['licenseRef'],
270  $licenseProperty['agentRef'],
271  $licenseProperty['matchId'],
272  array_key_exists('percentage', $licenseProperty) ? $licenseProperty['percentage'] : null
273  );
274  }
275 }
getLatestScannerDetectedMatches(ItemTreeBounds $itemTreeBounds)
Get all license id matches by agent for a given upload tree item.
__construct(LicenseDao $licenseDao, AgentDao $agentDao)
getScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get licenses detected by agents for a given upload tree item.
filterDetectedLicenses($agentDetectedLicenses, $agentLatestMap)
(A->B->C->X, A->B) => C->A->X
filterLatestScannerDetectedMatches($agentDetectedLicenses, $uploadId)
(A->B->C->X) => C->A->X if B=latestScannerId(A)
getLatestAgentIdPerAgent($uploadId, $agentNames)
Get map for agent name => agent id.
getScannerEvents(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get all scanner events that occurred on a given upload tree bound.
Contains business rules for FOSSology.
getScannedLicenses($details)
Get scanned license as a map of license-id => license-ref.
getScannerDetectedLicenseDetails(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get licenses match from agents for given upload tree items.
createAgentClearingEvent($licenseProperty)
Create a new AgentClearingEvent.