Completed
Pull Request — master (#32545)
by Tom
08:55
created

TransferOwnership::run()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 43
rs 8.6097
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Files\BackgroundJob;
23
24
use OC\BackgroundJob\QueuedJob;
25
use OCA\Files\Service\TransferOwnership\TransferOwnershipService;
26
use OCA\Files\Service\TransferOwnership\TransferRequestManager;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCP\Files\IRootFolder;
29
use OCP\Files\NotFoundException;
30
use OCP\ILogger;
31
use OCP\IUserManager;
32
33
class TransferOwnership extends QueuedJob {
34
35
	/** @var ILogger */
36
	protected $logger;
37
	/** @var TransferOwnershipService  */
38
	protected $service;
39
	/** @var IUserManager  */
40
	protected $userManager;
41
	/** @var TransferRequestManager  */
42
	protected $requestManager;
43
	/** @var IRootFolder  */
44
	protected $rootFolder;
45
46
	public function __construct(
47
		TransferOwnershipService $service,
48
		IUserManager $userManager,
49
		TransferRequestManager $requestManager,
50
		IRootFolder $rootFolder) {
51
		$this->service = $service;
52
		$this->userManager = $userManager;
53
		$this->requestManager = $requestManager;
54
		$this->rootFolder = $rootFolder;
55
	}
56
57
	public function execute($jobList, ILogger $logger = null) {
58
		$this->logger = $logger;
59
		parent::execute($jobList, $logger);
0 ignored issues
show
Compatibility introduced by
$jobList of type object<OCP\BackgroundJob\IJobList> is not a sub-type of object<OC\BackgroundJob\JobList>. It seems like you assume a concrete implementation of the interface OCP\BackgroundJob\IJobList to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
	}
61
62
	public function run($argument) {
63
		// Get the arguments;
64
		$arguments = json_decode($argument);
65
		try {
66
			$request = $this->requestManager->getRequestById($arguments->requestId);
67
		} catch (DoesNotExistException $e) {
68
			// Can happen if this gets deleted before the cron runs
69
			$this->logger->info("Transfer job called but request is missing");
70
			return;
71
		}
72
73
		// Call the transfer service
74
		$sourceUser = $this->userManager->get($request->getSourceUserId());
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
		$destinationUser = $this->userManager->get($request->getDestinationUserId());
0 ignored issues
show
Documentation Bug introduced by
The method getDestinationUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
77
		if ($sourceUser === null || $destinationUser === null) {
78
			$this->logger->error("Trasnfer job called but at least one of the users in the request are missing: source:{$request->getSourceUserId()} destination:{$request->getDestinationUserId()}", ['app' => 'files']);
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getDestinationUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
			return;
80
		}
81
82
		try {
83
			$sourcePath = $this->rootFolder->getUserFolder($request->getSourceUserId())
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
				->getById($request->getFileId())[0]
0 ignored issues
show
Documentation Bug introduced by
The method getFileId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
				->getInternalPath();
86
		} catch (NotFoundException $e) {
87
			$this->logger->error("Transfer job called but node no longer exists");
88
			$this->requestManager->deleteRequest($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<OCP\AppFramework\Db\Entity> is not a sub-type of object<OCA\Files\Service...ership\TransferRequest>. It seems like you assume a child class of the class OCP\AppFramework\Db\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
			return;
90
		}
91
92
93
		try {
94
			$this->service->transfer($sourceUser, $destinationUser, $sourcePath);
95
		} catch (\Exception $e) {
96
			$this->logger->logException($e, ['app' =>  'files']);
97
			$this->requestManager->actionRequestFailure($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<OCP\AppFramework\Db\Entity> is not a sub-type of object<OCA\Files\Service...ership\TransferRequest>. It seems like you assume a child class of the class OCP\AppFramework\Db\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
98
			return;
99
		}
100
101
		$this->logger->info("Finished transfer of $sourcePath from {$sourceUser->getUID()} to {$destinationUser->getUID()}");
102
		$this->requestManager->actionRequest($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<OCP\AppFramework\Db\Entity> is not a sub-type of object<OCA\Files\Service...ership\TransferRequest>. It seems like you assume a child class of the class OCP\AppFramework\Db\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
103
104
	}
105
106
}