FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AgentLicenseEventProcessor.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 
21 
27 {
30  private $latestAgentMapCache = array();
33  private $licenseDao;
36  private $agentDao;
37 
44  {
45  $this->licenseDao = $licenseDao;
46  $this->agentDao = $agentDao;
47  }
48 
55  public function getScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
56  {
57  $details = $this->getScannerDetectedLicenseDetails($itemTreeBounds, $usageId);
58 
59  return $this->getScannedLicenses($details);
60  }
61 
82  protected function getScannerDetectedLicenseDetails(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
83  {
84  $agentDetectedLicenses = array();
85 
86  $licenseFileMatches = $this->licenseDao->getAgentFileLicenseMatches($itemTreeBounds, $usageId);
87 
88  foreach ($licenseFileMatches as $licenseMatch) {
89  $licenseRef = $licenseMatch->getLicenseRef();
90  $licenseId = $licenseRef->getId();
91  if ($licenseRef->getShortName() === "No_license_found") {
92  continue;
93  }
94  $agentRef = $licenseMatch->getAgentRef();
95  $agentName = $agentRef->getAgentName();
96  $agentId = $agentRef->getAgentId();
97 
98  $agentDetectedLicenses[$agentName][$agentId][$licenseId][] = array(
99  'id' => $licenseId,
100  'licenseRef' => $licenseRef,
101  'agentRef' => $agentRef,
102  'matchId' => $licenseMatch->getLicenseFileId(),
103  'percentage' => $licenseMatch->getPercentage()
104  );
105  }
106 
107  return $this->filterLatestScannerDetectedMatches($agentDetectedLicenses, $itemTreeBounds->getUploadId());
108  }
109 
115  public function getLatestScannerDetectedMatches(ItemTreeBounds $itemTreeBounds)
116  {
117  $agentDetectedLicenses = array();
118 
119  $licenseFileMatches = $this->licenseDao->getAgentFileLicenseMatches($itemTreeBounds);
120 
121  foreach ($licenseFileMatches as $licenseMatch) {
122  $licenseRef = $licenseMatch->getLicenseRef();
123  $licenseId = $licenseRef->getId();
124  if ($licenseRef->getShortName() === "No_license_found") {
125  continue;
126  }
127  $agentRef = $licenseMatch->getAgentRef();
128  $agentName = $agentRef->getAgentName();
129  $agentId = $agentRef->getAgentId();
130 
131  $agentDetectedLicenses[$agentName][$agentId][$licenseId][] = $licenseMatch;
132  }
133 
134  return $this->filterLatestScannerDetectedMatches($agentDetectedLicenses, $itemTreeBounds->getUploadId());
135  }
136 
143  protected function filterLatestScannerDetectedMatches($agentDetectedLicenses, $uploadId)
144  {
145  $agentNames = array_keys($agentDetectedLicenses);
146  if (empty($agentNames)) {
147  return array();
148  }
149 
150  $latestAgentIdPerAgent = $this->getLatestAgentIdPerAgent($uploadId, $agentNames);
151  $latestAgentDetectedLicenses = $this->filterDetectedLicenses($agentDetectedLicenses, $latestAgentIdPerAgent);
152  return $latestAgentDetectedLicenses;
153  }
154 
163  private function getLatestAgentIdPerAgent($uploadId, $agentNames)
164  {
165  if (!array_key_exists($uploadId,$this->latestAgentMapCache)
166  || count(array_diff_key($agentNames, $this->latestAgentMapCache[$uploadId]))>0) {
167  $latestScannerProxy = new LatestScannerProxy($uploadId, $agentNames, "latest_scanner$uploadId");
168  $latestAgentIdPerAgent = $latestScannerProxy->getNameToIdMap();
169  foreach ($latestAgentIdPerAgent as $agentName=>$agentMap) {
170  $this->latestAgentMapCache[$uploadId][$agentName] = $agentMap;
171  }
172  }
173  if (array_key_exists($uploadId, $this->latestAgentMapCache)) {
174  return $this->latestAgentMapCache[$uploadId];
175  } else {
176  return array();
177  }
178  }
179 
186  protected function filterDetectedLicenses($agentDetectedLicenses, $agentLatestMap)
187  {
188  $latestAgentDetectedLicenses = array();
189 
190  foreach ($agentDetectedLicenses as $agentName => $licensesFoundPerAgentId) {
191  if (!array_key_exists($agentName, $agentLatestMap)) {
192  continue;
193  }
194  $latestAgentId = $agentLatestMap[$agentName];
195  if (!array_key_exists($latestAgentId, $licensesFoundPerAgentId)) {
196  continue;
197  }
198  foreach ($licensesFoundPerAgentId[$latestAgentId] as $licenseId => $properties) {
199  $latestAgentDetectedLicenses[$licenseId][$agentName] = $properties;
200  }
201  }
202 
203  return $latestAgentDetectedLicenses;
204  }
205 
211  public function getScannedLicenses($details)
212  {
213  $licenses = array();
214 
215  foreach ($details as $licenseId => $agentEntries) {
216  foreach ($agentEntries as $matchProperties) {
217  $licenses[$licenseId] = $matchProperties[0]['licenseRef'];
218  break;
219  }
220  }
221 
222  return $licenses;
223  }
224 
231  public function getScannerEvents(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
232  {
233  $agentDetails = $this->getScannerDetectedLicenseDetails($itemTreeBounds, $usageId);
234 
235  $result = array();
236  foreach ($agentDetails as $licenseId => $properties) {
237  $agentClearingEvents = array();
238  foreach ($properties as $licenseProperties) {
239  foreach ($licenseProperties as $licenseProperty) {
240  $agentClearingEvents[] = $this->createAgentClearingEvent($licenseProperty);
241  }
242  }
243 
244  $result[$licenseId] = $agentClearingEvents;
245  }
246  return $result;
247  }
248 
255  private function createAgentClearingEvent($licenseProperty)
256  {
257  return new AgentClearingEvent(
258  $licenseProperty['licenseRef'],
259  $licenseProperty['agentRef'],
260  $licenseProperty['matchId'],
261  array_key_exists('percentage', $licenseProperty) ? $licenseProperty['percentage'] : null
262  );
263  }
264 }
getLatestAgentIdPerAgent($uploadId, $agentNames)
Get map for agent name => agent id.
getScannedLicenses($details)
Get scanned license as a map of license-id => license-ref.
createAgentClearingEvent($licenseProperty)
Create a new AgentClearingEvent.
getScannerDetectedLicenseDetails(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get licenses match from agents for given upload tree items.
getScannerEvents(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get all scanner events that occurred on a given upload tree bound.
getScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
Get licenses detected by agents for a given upload tree item.
filterLatestScannerDetectedMatches($agentDetectedLicenses, $uploadId)
(A->B->C->X) => C->A->X if B=latestScannerId(A)
__construct(LicenseDao $licenseDao, AgentDao $agentDao)
filterDetectedLicenses($agentDetectedLicenses, $agentLatestMap)
(A->B->C->X, A->B) => C->A->X
getLatestScannerDetectedMatches(ItemTreeBounds $itemTreeBounds)
Get all license id matches by agent for a given upload tree item.
Contains business rules for FOSSology.